byte: for bytes
short/int/long: for whole numbers
float/double: for decimal numbers
char: for a single character
boolean: for true/false values
+: addition
-: subtraction
*: multiplication
/: division
%: modulus (remainder after division)
++: increment by 1
--: decrement by 1
+=: add and assign
-=: subtract and assign
*=: multiply and assign
/=: divide and assign
%=: modulus and assign
if (condition) { code block }: execute code block if condition is true
else if (condition) { code block }: execute code block if previous condition is false and current condition is true
else { code block }: execute code block if all previous conditions are false
for (initialization; condition; increment/decrement) { code block }: execute code block repeatedly until condition is false
while (condition) { code block }: execute code block repeatedly while condition is true
do { code block } while (condition);: execute code block at least once, and repeatedly while condition is true
dataType[] arrayName = new dataType[arraySize];: declare and initialize an array
arrayName[index]: access a specific element in the array
arrayName.length: get the number of elements in the array
class ClassName { class body }: define a class
ClassName objectName = new ClassName();: create an object of a class
objectName.methodName();: call a method of an object
public: access modifier that allows other classes to access the class or method
private: access modifier that restricts access to only within the class
class Subclass extends Superclass { class body }: define a subclass that inherits from a superclass
super.methodName();: call a method from the superclass
interface InterfaceName { interface body }: define an interface
class ClassName implements InterfaceName { class body }: implement an interface in a class
This is just a brief overview of some of the key concepts in Java. For more detailed information, refer to the official Java documentation at https://docs.oracle.com/javase/tutorial/.
try { code block } catch (ExceptionType e) { code block } finally { code block }: handle exceptions that might occur in the code block
throw new ExceptionType("message");: throw an exception with a specific message
throws ExceptionType: declare that a method might throw a specific exception
try-with-resources: automatically close resources after use
try (ResourceType resource = new ResourceType()) {
// code block
} catch (ExceptionType e) {
// code block
}
List
ArrayList: a resizable array implementation of the List interface
List<String> list = new ArrayList<>();
list.add("value");
String value = list.get(0);
LinkedList: a doubly-linked list implementation of the List interface
List<String> list = new LinkedList<>();
list.add("value");
String value = list.get(0);
Set
HashSet: an unordered implementation of the Set interface that uses a hash table for storage
Set<String> set = new HashSet<>();
set.add("value");
boolean containsValue = set.contains("value");
TreeSet: a sorted implementation of the Set interface that uses a binary tree for storage
Set<String> set = new TreeSet<>();
set.add("value");
boolean containsValue = set.contains("value");
Map
HashMap: an unordered implementation of the Map interface that uses a hash table for storage
Map<String, String> map = new HashMap<>();
map.put("key", "value");
String value = map.get("key");;
TreeMap: a sorted implementation of the Map interface that uses a binary tree for storage
Set<String> set = new TreeSet<>();
set.add("value");
boolean containsValue = set.contains("value");
Queue
LinkedList: a doubly-linked list implementation of the Queue interface
Queue<String, String> queue = new LinkedList<>();
queue.add("value");
String value = queue.get("key");
PriorityQueue: a priority queue implementation of the Queue interface that orders elements based on a Comparator or their natural order
Queue<String> queue = new PriorityQueue<>();
queue.add("value");
String value = queue.peek();
Stack
Stack: a last-in, first-out (LIFO) implementation of the List interface
Stack<String> queue = new Stack<>();
Stack.push("value");
String value = stack.pop();
This is just a brief overview of some of the key collections in Java. For more detailed information, refer to the official Java documentation at https://docs.oracle.com/javase/tutorial/collections/.
Thread: a class that represents a thread of execution
Runnable: a functional interface that represents a task that can be executed in a thread
start(): start a thread
join(): wait for a thread to finish
synchronized: ensure that only one thread can access a block of code at a time
class MyThread extends Thread {
public void run() {
// code block
}
}
class MyRunnable implements Runnable {
public void run() {
// code block
}
}
Thread thread = new MyThread();
thread.start();
Runnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();
synchronized (object) {
// code block
}
(parameters) -> { code block }: define a lambda expression
FunctionalInterfaceName variableName = (parameters) -> { code block };: assign a lambda expression to a variable of a functional interface type
interface MyInterface {
void doSomething(int parameter);
}
MyInterface myObject = (parameter) -> {
// code block
};
class ClassName
T methodName(T parameter) { code block }: define a generic method with a type parameter T
ClassName
class MyGenericClass<T> {
private T value;
public MyGenericClass(T value) {
this.value = value;
}
public T getValue() {
return value;
}
};
MyGenericClass<String> myObject = new MyGenericClass<>("value");
@AnnotationName: apply an annotation to a class, method, field, or parameter
@Retention(RetentionPolicy.RUNTIME): specify the retention policy of an annotation (e.g. source, class, runtime)
@Target(ElementType.TYPE): specify the element types to which an annotation can be applied (e.g. class, method, field, parameter)
@Target(ElementType.TYPE)
@interface MyAnnotation {
String value();
}
@MyAnnotation("value")
class MyClass {
// class body
}