Professional Documents
Culture Documents
Oops Java Cheatsheet 2023
Oops Java Cheatsheet 2023
Oops Java Cheatsheet 2023
Start your Java day with Hello World Operand What they do for (int i: someArray) {}
program = Assign value while (something) {}
public class HelloWorld { do {something} while (true)
== Check value/address similarity
public static void main(S‐
> More than
tring[] args) { Prime number function
// Prints "Hello, World" to the >= More than or equals
if (n < 2) { return false; }
terminal window. >>> Move bit to the right by
for (int i=2; i <= n/i; i++)
System.out.println("Hello, ++ Increment by 1 if (n%i == 0) return false;
World");
inverse of these operands still working the return true;
} same.
returns a boolean
} For example : != is not equal
When you want to run the program, choose
String Pool - Optimizations
this class as main class. Defining variable
String pool is created to make the same
Defining new variable attributes
Run your code value string use the same address. By
int x = 12;
doing that, it will save memory and time for
Compile from single class up HelloWorld int x; // will be defined as 0
compiler to do stuff
class Define by creating new instances Basic testing
javac HelloWorld.java String x = new String; String s1 = "Hello World";
java HelloWorld Type Casting (decreasing bit use) String s2 = "Hello World;
Compile from multiple classes and choose Expanding data types will not require type
Check it using "=="
main class casting. Narrowing does.
System.out.println(s1 ==
javac *.java double x = 10; // Expanding data
s2);
java HelloWorld // HelloWorld types
True
is your preferred main class int y = (int) 10.222222; //
"==" will check its address
Narrowing data types
Allocate a new address using new
Variables
String s1 = "Hello World";
Type Default Memory Allocation Conditions
String s2 = new String;
Value If statement s2 = "Hello World";
byte 0 8 bits if (statement) {} System.out.println(s1 == s2);
short 0 16 bits If - else statement False
if (statement) {} else{} Allocate new address by changing its value
int 0 32 bits
String s1 = "Hello World";
long 0L 64 bits
Switch String s2 = "Hello World";
float 0.0F 32 bits (decimal)
switch (num) { s2 = "Hello Thailand";
double 0.00D 64 bits (decimal)
case 1: doSomething(); System.out.println(s1 == s2);
boolean False varies on impliment break; False
String NULL depends on default: doThis();
character count break;
char \u0000 16 bits (unicode) }
Naming should be regulated for easier But will be created automatically by not
recogition from others writing any constructor
Use Upper Camel Case for classes : Create an argument-defined constructor
VelocityResponseWriter <modifier> Person (String
Use Lower Case for packages: com.co‐ name) {
mpany.project.ui this.name = name;
Use Lower Camel Case for variables: }
studentName
Use Upper Case for constants: MAX_PA‐ - Java uses <default>
modifier when Abstract Class
RAMETER_COUNT = 100 not assigning any. Abstract is a type of class but it can consist
Use Camel Case for enum class names - public modifier allows same class of incomplete methods .
Use Upper Case for enum values access Create new abstract
Don't use '_' anywhere except constants - Works in inherited class means itself and <access_modifier>
and enum values (which are constants). the classes that inherit from it. abstract class HelloWorld () {}
Encapsulation allows individual methods to Java will not allow this to be run, because it Create a Scanner object
have different access modifier. cannot determine the value. Scanner sc = new Scanner(S‐
Creating setters and getters is one way to ystem.in);
use encapsulation Override Accept input
For example double d = sc.nextDouble()
When you have inherit some of the class
private void setTime(int hour,
from parents, but you want to do something
int minuite, int second){ different. In override feature, all the subcla‐ java.lang.Math
this.hour = hour; ss/class object will use the newer method. Methods Usage
this.minuite = minuite; To make sure JDK knows what you are
Math.max(<va‐ Return maximum
this.second = second; doing, type @Override in front of the
lue1>, <va‐ value
} public name. If the override is unsuccessful,
lue2>)
JDK will returns error.
Inheritance Example of overriden helloWorld() method : Math.min(<va‐ Return minimum
startsWith(String Test if string starts with int indexOf(Object Find the #index
java/lang/String.html