int x = 10; | Integer (32-bit) |
long l = 10L; | Long (64-bit) |
double d = 3.14; | Double (64-bit) |
float f = 3.14f; | Float (32-bit) |
boolean b = true; | Boolean |
char c = 'A'; | Character (16-bit) |
String s = "hello"; | String object |
final int MAX = 100; | Constant |
int[] arr = new int[5]; | Array declaration |
int[] arr = {1, 2, 3}; | Array initialization |
arr.length | Array length |
int[][] matrix = new int[3][3]; | 2D array |
Arrays.sort(arr) | Sort array |
Arrays.copyOf(arr, len) | Copy array |
Arrays.fill(arr, val) | Fill array |
s.length() | String length |
s.charAt(i) | Get character |
s.substring(start, end) | Substring |
s.indexOf("text") | Find index |
s.split(",") | Split string |
s.trim() | Trim whitespace |
s.toLowerCase() / toUpperCase() | Change case |
String.format("%d", n) | Format string |
public class MyClass { } | Class definition |
private int field; | Private field |
public MyClass() { } | Constructor |
this.field = value; | Access instance field |
static int count; | Static field |
public static void main(String[] args) | Main method |
class Child extends Parent | Extend class |
super() | Call parent constructor |
super.method() | Call parent method |
@Override | Override annotation |
interface MyInterface { } | Interface definition |
class MyClass implements Interface | Implement interface |
abstract class MyClass { } | Abstract class |
abstract void method(); | Abstract method |
public | Accessible everywhere |
protected | Same package + subclasses |
default (no modifier) | Same package only |
private | Same class only |
final | Cannot be overridden/extended |
static | Class-level member |
List<String> list = new ArrayList<>(); | Create ArrayList |
List<String> list = new LinkedList<>(); | Create LinkedList |
list.add(item) | Add element |
list.get(index) | Get by index |
list.set(index, item) | Set at index |
list.remove(index) | Remove by index |
list.size() | Get size |
list.contains(item) | Check contains |
Set<String> set = new HashSet<>(); | Create HashSet |
Set<String> set = new TreeSet<>(); | Create TreeSet (sorted) |
Map<String, Integer> map = new HashMap<>(); | Create HashMap |
map.put(key, value) | Put key-value |
map.get(key) | Get by key |
map.getOrDefault(key, default) | Get with default |
map.containsKey(key) | Check key exists |
map.keySet() / values() / entrySet() | Get keys/values/entries |
Queue<Integer> q = new LinkedList<>(); | Create Queue |
Deque<Integer> dq = new ArrayDeque<>(); | Create Deque |
q.offer(item) / q.add(item) | Enqueue |
q.poll() / q.remove() | Dequeue |
q.peek() | Peek front |
Stack<Integer> stack = new Stack<>(); | Create Stack |
stack.push(item) | Push |
stack.pop() | Pop |
list.stream() | Stream from collection |
Arrays.stream(arr) | Stream from array |
Stream.of(1, 2, 3) | Stream from values |
Stream.iterate(0, n -> n + 1) | Infinite stream |
Stream.generate(Math::random) | Generate stream |
IntStream.range(0, 10) | Range stream |
.filter(x -> x > 0) | Filter elements |
.map(x -> x * 2) | Transform elements |
.flatMap(list -> list.stream()) | Flatten nested |
.distinct() | Remove duplicates |
.sorted() | Sort elements |
.limit(n) | Limit to n elements |
.skip(n) | Skip n elements |
.peek(System.out::println) | Peek for debugging |
.collect(Collectors.toList()) | Collect to List |
.collect(Collectors.toSet()) | Collect to Set |
.collect(Collectors.toMap(k, v)) | Collect to Map |
.collect(Collectors.joining(",")) | Join strings |
.forEach(System.out::println) | For each element |
.reduce(0, (a, b) -> a + b) | Reduce to single value |
.count() | Count elements |
.anyMatch(x -> x > 0) | Any match predicate |
(a, b) -> a + b | Basic lambda |
x -> x * 2 | Single param (no parens) |
() -> "hello" | No params |
(a, b) -> { return a + b; } | Block body |
String::length | Method reference |
System.out::println | Instance method ref |
MyClass::new | Constructor reference |
Function<T, R> | T -> R transformation |
Predicate<T> | T -> boolean test |
Consumer<T> | T -> void action |
Supplier<T> | () -> T provider |
BiFunction<T, U, R> | (T, U) -> R |
UnaryOperator<T> | T -> T |
BinaryOperator<T> | (T, T) -> T |
Optional.of(value) | Create with value |
Optional.ofNullable(value) | Create nullable |
Optional.empty() | Create empty |
opt.isPresent() | Check if present |
opt.ifPresent(consumer) | If present do action |
opt.orElse(default) | Get or default |
opt.orElseThrow() | Get or throw |
opt.map(func).flatMap(func) | Transform optional |
try { } catch (Exception e) { } | Basic try-catch |
catch (IOException | SQLException e) | Multi-catch |
finally { } | Finally block |
try (Resource r = new Resource()) | Try-with-resources |
throw new Exception("msg") | Throw exception |
throws IOException | Declare throws |
Exception | Checked exception base |
RuntimeException | Unchecked exception base |
NullPointerException | Null reference |
IndexOutOfBoundsException | Index out of bounds |
IllegalArgumentException | Invalid argument |
IOException | I/O error |