Java Cheat Sheet
Java syntax, patterns and idioms, searchable and copy-ready, with the Java version noted on newer features.
By Bogdan SanduUpdated August 4, 2026
Basics
Hello World
Classic Java program (all versions)
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Compact source file: no class declaration needed (Java 25+)
void main() {
IO.println("Hello, World!");
}Primitive Types
Integer types
byte b = 127; // 8-bit [-128, 127]
short s = 32000; // 16-bit
int i = 2_147_483_647; // 32-bit (use _ for readability)
long l = 9_876_543_210L; // 64-bit, suffix LFloating-point types
float f = 3.14f; // 32-bit, suffix f
double d = 3.14159; // 64-bit (default)Other primitives
boolean flag = true;
char c = 'A'; // 16-bit UnicodeVariables & Constants
Variable declaration
int age = 30;
String name = "Alice";
double pi = 3.14159;Local type inference with var (Java 10+)
var score = 98.5; // inferred as double
var names = new ArrayList<String>(); // inferred typeConstants with final
final int MAX_SIZE = 100;
static final double PI = 3.14159;Arrays
Declare and initialize
int[] nums = {1, 2, 3, 4, 5};
String[] names = new String[3];
names[0] = "Alice";2D array
int[][] grid = new int[3][3];
int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};Useful array operations
int len = nums.length;
int[] cpy = Arrays.copyOf(nums, nums.length);
Arrays.sort(nums);
int idx = Arrays.binarySearch(nums, 3);Type Casting
Widening (automatic) and narrowing (explicit)
int i = 42;
double d = i; // widening, automatic
int j = (int) d; // narrowing, explicit castString to/from number
int n = Integer.parseInt("42");
double v = Double.parseDouble("3.14");
String s = String.valueOf(100); // or Integer.toString(100)OOP
Classes & Objects
Class with constructor and methods
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public void setName(String n) { this.name = n; }
@Override
public String toString() {
return "Person{name=" + name + ", age=" + age + "}";
}
}Inheritance
Extending a class
public class Employee extends Person {
private String role;
public Employee(String name, int age, String role) {
super(name, age); // call parent constructor
this.role = role;
}
@Override
public String toString() {
return super.toString() + ", role=" + role;
}
}Interfaces & Abstract Classes
Interface with default method
public interface Drawable {
void draw(); // abstract
default String getType() { // default impl (Java 8+)
return "shape";
}
}Abstract class
public abstract class Shape {
abstract double area(); // subclass must implement
void describe() { // concrete method
System.out.println("Area: " + area());
}
}Implementing an interface
public class Circle extends Shape implements Drawable {
double r;
Circle(double r) { this.r = r; }
@Override public double area() { return Math.PI * r * r; }
@Override public void draw() { System.out.println("O"); }
}Enums
Basic enum
public enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }
Day d = Day.MON;
d.name(); // "MON"
d.ordinal(); // 0Enum with fields and methods
public enum Planet {
EARTH(5.97e24, 6.37e6),
MARS (6.39e23, 3.39e6);
final double mass, radius;
Planet(double m, double r) { mass=m; radius=r; }
double surfaceGravity() {
return 6.67e-11 * mass / (radius * radius);
}
}Annotations
Common built-in annotations
@Override // must override a supertype method
@Deprecated // marks API as outdated
@SuppressWarnings("unchecked") // suppress compiler warnings
@FunctionalInterface // ensures exactly one abstract methodCustom annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Timed {
String value() default ""; // optional element
}
// Usage
@Timed("myOperation")
public void process() { /* ... */ }Comparable vs Comparator
Comparable - natural ordering (implement on the class)
public class Student implements Comparable<Student> {
String name; int grade;
@Override
public int compareTo(Student other) {
return Integer.compare(this.grade, other.grade);
}
}
Collections.sort(students); // uses compareToComparator - external, flexible ordering
// Lambda comparator
students.sort(Comparator.comparing(s -> s.name));
// Chained comparators
students.sort(
Comparator.comparingInt((Student s) -> s.grade)
.reversed()
.thenComparing(s -> s.name)
);Generics
Generic class and method
public class Pair<A, B> {
private final A first;
private final B second;
public Pair(A a, B b) { first=a; second=b; }
public A getFirst() { return first; }
public B getSecond() { return second; }
}
public <T extends Comparable<T>> T max(T a, T b) {
return a.compareTo(b) >= 0 ? a : b;
}Bounded wildcards
// ? extends - read (covariant)
double sum(List<? extends Number> list) {
return list.stream().mapToDouble(Number::doubleValue).sum();
}
// ? super - write (contravariant)
void addNumbers(List<? super Integer> list) {
list.add(1); list.add(2);
}Control Flow
Conditionals
if / else if / else
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else {
grade = "C";
}Ternary operator
String result = (x > 0) ? "positive" : "non-positive";switch statement (classic)
switch (day) {
case "MON": case "TUE":
System.out.println("Weekday"); break;
case "SAT": case "SUN":
System.out.println("Weekend"); break;
default:
System.out.println("Unknown");
}switch expression (Java 14+)
int numLetters = switch (day) {
case "MON", "FRI", "SUN" -> 6;
case "TUE" -> 7;
case "THU", "SAT" -> 8;
case "WED" -> 9;
default -> throw new IllegalArgumentException(day);
};Loops
for loop
for (int i = 0; i < 10; i++) {
System.out.println(i);
}Enhanced for-each
for (String name : names) {
System.out.println(name);
}while and do-while
while (condition) { /* ... */ }
do {
/* executes at least once */
} while (condition);break, continue, labeled break
outer:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 2) continue; // skip j=2
if (i == 3) break outer; // exit both loops
}
}Collections
List
ArrayList - dynamic array
List<String> list = new ArrayList<>();
list.add("Alice");
list.add("Bob");
list.get(0); // "Alice"
list.size(); // 2
list.remove("Bob");
list.contains("Alice"); // true
list.set(0, "Carol"); // replace at indexImmutable list (Java 9+)
List<String> names = List.of("Alice", "Bob", "Carol");Map
HashMap - key-value pairs
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.get("a"); // 1
map.getOrDefault("z", 0); // 0
map.containsKey("a"); // true
map.remove("a");
map.putIfAbsent("b", 2); // only if key absent
for (Map.Entry<String,Integer> e : map.entrySet()) {
System.out.println(e.getKey() + "=" + e.getValue());
}Immutable map (Java 9+)
Map<String,Integer> m = Map.of("a", 1, "b", 2);Set & Queue
HashSet - unique elements
Set<Integer> set = new HashSet<>(List.of(1,2,3,2));
set.size(); // 3 (duplicates removed)
set.add(4);
set.contains(2); // trueArrayDeque as stack and queue
Deque<Integer> stack = new ArrayDeque<>();
stack.push(1); stack.push(2);
stack.pop(); // 2, LIFO
Queue<Integer> q = new LinkedList<>();
q.offer(1); q.offer(2);
q.poll(); // 1, FIFOStrings
String Basics
Common String methods
String s = "Hello, World!";
s.length(); // 13
s.charAt(0); // 'H'
s.indexOf("World"); // 7
s.substring(7); // "World!"
s.substring(7, 12); // "World"
s.toLowerCase(); // "hello, world!"
s.toUpperCase(); // "HELLO, WORLD!"
s.trim(); // strips leading/trailing whitespace
s.replace("World", "Java"); // "Hello, Java!"
s.contains("World"); // true
s.startsWith("Hello"); // true
s.split(", "); // ["Hello", "World!"]String comparison - always use equals()
String a = "hello", b = "hello";
a.equals(b); // true -- correct
a.equalsIgnoreCase(b); // true
a == b; // unreliable, avoidNew String methods (Java 11+)
s.isBlank(); // true if empty or whitespace
s.lines(); // Stream of lines
s.repeat(3); // "abcabcabc"
" hi ".stripLeading(); // "hi "
" hi ".stripTrailing();// " hi"String Formatting & Building
String.format and printf
String msg = String.format("Name: %s, Age: %d", name, age);
System.out.printf("Pi = %.2f%n", Math.PI);Text block (Java 15+)
String json = """
{
"name": "Alice",
"age": 30
}
""";StringBuilder - mutable, efficient for loops
StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(", ").append("World");
sb.insert(5, "!");
String result = sb.toString(); // "Hello!, World"String.join
String joined = String.join(", ", "a", "b", "c");
String fromList = String.join("-", List.of("x","y","z"));Streams & Lambda
Lambda Expressions
Lambda syntax
// (params) -> expression
Runnable r = () -> System.out.println("run");
// (params) -> { block }
Comparator<String> cmp = (a, b) -> {
return a.compareTo(b);
};
// Method reference
list.forEach(System.out::println);Common functional interfaces
Predicate<Integer> isEven = n -> n % 2 == 0;
Function<String, Integer> len = String::length;
Consumer<String> print = System.out::println;
Supplier<List<String>> newList = ArrayList::new;
BiFunction<Integer,Integer,Integer> add = (a,b) -> a+b;Stream API
filter, map, collect
List<String> result = names.stream()
.filter(n -> n.startsWith("A"))
.map(String::toUpperCase)
.sorted()
.collect(Collectors.toList());reduce and count
int sum = numbers.stream()
.reduce(0, Integer::sum);
long count = names.stream()
.filter(n -> n.length() > 3)
.count();groupingBy and joining collectors
Map<Integer, List<String>> byLen = names.stream()
.collect(Collectors.groupingBy(String::length));
String csv = names.stream()
.collect(Collectors.joining(", "));findFirst and Optional
Optional<String> first = names.stream()
.filter(n -> n.startsWith("B"))
.findFirst();
first.ifPresent(System.out::println);
String val = first.orElse("default");Primitive streams
IntStream.range(0, 5).forEach(System.out::println); // 0..4
IntStream.rangeClosed(1, 5).sum(); // 15
DoubleStream.of(1.0, 2.0, 3.0).average(); // OptionalDoubleExceptions
try / catch / finally
Basic exception handling
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Always runs");
}Multi-catch (Java 7+)
try {
// ...
} catch (IOException | SQLException e) {
System.out.println("IO or SQL error: " + e);
}try-with-resources - auto close
try (BufferedReader br = new BufferedReader(
new FileReader("file.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} // br.close() called automaticallyCustom Exceptions
Checked exception
public class InsufficientFundsException extends Exception {
public InsufficientFundsException(String msg) {
super(msg);
}
}
void withdraw(double amount) throws InsufficientFundsException {
if (amount > balance)
throw new InsufficientFundsException("Not enough funds");
}Unchecked exception (runtime)
public class AppException extends RuntimeException {
public AppException(String msg, Throwable cause) {
super(msg, cause);
}
}Concurrency
Threads
Creating and starting threads
// Lambda runnable
Thread t1 = new Thread(() -> System.out.println("Thread 1"));
t1.start();
// Implement Runnable
Runnable task = () -> System.out.println("Task running");
new Thread(task).start();
// Join - wait for thread to finish
t1.join();synchronized - prevent race conditions
public class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public synchronized int get() {
return count;
}
}ExecutorService
Thread pool with ExecutorService
ExecutorService pool = Executors.newFixedThreadPool(4);
pool.submit(() -> System.out.println("Task 1"));
pool.submit(() -> System.out.println("Task 2"));
pool.shutdown();
pool.awaitTermination(10, TimeUnit.SECONDS);Future - get result from async task
ExecutorService pool = Executors.newSingleThreadExecutor();
Future<Integer> future = pool.submit(() -> {
Thread.sleep(100);
return 42;
});
int result = future.get(); // blocks until done
pool.shutdown();CompletableFuture
Async pipeline with CompletableFuture
CompletableFuture<String> cf = CompletableFuture
.supplyAsync(() -> "hello") // run async
.thenApply(String::toUpperCase) // transform
.thenApply(s -> s + " WORLD"); // transform again
String result = cf.get(); // "HELLO WORLD"
cf.exceptionally(ex -> "default on error");Combining futures
CompletableFuture<String> a = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> b = CompletableFuture.supplyAsync(() -> "World");
CompletableFuture<String> combined = a.thenCombine(b, (x,y) -> x + " " + y);
System.out.println(combined.get()); // "Hello World"I/O & Files
Reading Files
Read all lines (NIO)
Path path = Path.of("data.txt");
// Read all lines into a List
List<String> lines = Files.readAllLines(path);
// Read as a single String (Java 11+)
String content = Files.readString(path);
// Stream lines lazily
try (Stream<String> stream = Files.lines(path)) {
stream.filter(l -> !l.isBlank()).forEach(System.out::println);
}BufferedReader for large files
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}Writing Files
Write with Files (NIO)
Path path = Path.of("output.txt");
Files.writeString(path, "Hello
"); // Java 11+
Files.write(path, List.of("line1", "line2"));
Files.writeString(path, "more
", StandardOpenOption.APPEND);BufferedWriter
try (BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt"))) {
bw.write("Line 1");
bw.newLine();
bw.write("Line 2");
} catch (IOException e) {
e.printStackTrace();
}Path & Files Utilities
Common Path and Files operations
Path p = Path.of("/home/user/docs/file.txt");
p.getFileName(); // file.txt
p.getParent(); // /home/user/docs
p.toAbsolutePath();
Files.exists(p);
Files.isDirectory(p);
Files.size(p); // bytes
Files.createDirectories(Path.of("a/b/c"));
Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING);
Files.delete(p);
Files.move(src, dst);Utilities
Math
Common Math methods
Math.abs(-5); // 5
Math.max(3, 7); // 7
Math.min(3, 7); // 3
Math.pow(2, 10); // 1024.0
Math.sqrt(16); // 4.0
Math.floor(3.9); // 3.0
Math.ceil(3.1); // 4.0
Math.round(3.5); // 4
Math.random(); // [0.0, 1.0)
Math.PI; // 3.14159...
Math.log(Math.E); // 1.0Collections Utility Class
Sorting, searching, and shuffling
List<Integer> nums = new ArrayList<>(List.of(3,1,4,1,5,9));
Collections.sort(nums);
Collections.sort(nums, Comparator.reverseOrder());
Collections.shuffle(nums);
Collections.reverse(nums);
Collections.max(nums);
Collections.min(nums);
Collections.frequency(nums, 1); // count of value 1
Collections.unmodifiableList(nums); // read-only viewObjects Utility Class
Null-safe helpers
Objects.isNull(x); // true if x == null
Objects.nonNull(x); // true if x != null
Objects.requireNonNull(x, "msg"); // throws NPE if null
Objects.requireNonNullElse(x, defaultVal);
Objects.toString(x, "fallback"); // null-safe toString
Objects.equals(a, b); // null-safe equals
Objects.hash(a, b, c); // composite hash codeRandom Numbers
java.util.Random
Random rng = new Random();
rng.nextInt(100); // 0..99
rng.nextDouble(); // 0.0..1.0
rng.nextBoolean();
rng.nextLong();ThreadLocalRandom (preferred in concurrent code)
int n = ThreadLocalRandom.current().nextInt(1, 101); // 1..100Modern Java
Records
Immutable data class in one line
public record Point(int x, int y) {}
Point p = new Point(3, 4);
p.x(); // 3
p.y(); // 4
p.toString(); // "Point[x=3, y=4]"
p.equals(new Point(3, 4)); // trueRecord with compact constructor and extra method
public record Range(int min, int max) {
Range { // compact constructor
if (min > max) throw new IllegalArgumentException();
}
int span() { return max - min; }
}Sealed Classes
Restrict which classes can extend
public sealed interface Shape
permits Circle, Rectangle, Triangle {}
public record Circle(double radius) implements Shape {}
public record Rectangle(double w, double h) implements Shape {}
public record Triangle(double base, double h) implements Shape {}Pattern Matching
instanceof pattern matching (Java 16+)
// Old style
if (obj instanceof String) {
String s = (String) obj;
System.out.println(s.length());
}
// Pattern matching - s is already typed
if (obj instanceof String s) {
System.out.println(s.length());
}switch pattern matching (Java 21+)
double area = switch (shape) {
case Circle c -> Math.PI * c.radius() * c.radius();
case Rectangle r -> r.w() * r.h();
case Triangle t -> 0.5 * t.base() * t.h();
}; // compiler verifies exhaustiveness with sealed typesOptional
Creating and using Optional
Optional<String> opt = Optional.of("value");
Optional<String> empty = Optional.empty();
Optional<String> maybe = Optional.ofNullable(nullableRef);
opt.isPresent();
opt.get(); // throws if empty
opt.orElse("default");
opt.orElseGet(() -> computeDefault());
opt.orElseThrow(() -> new RuntimeException());
opt.map(String::toUpperCase); // Optional["VALUE"]
opt.filter(s -> s.length() > 3);
opt.ifPresent(System.out::println);Nothing matches. Try a single word.
What Is a Java Cheat Sheet?
A Java cheat sheet is a condensed, scannable reference covering Java syntax, data types, control flow, object-oriented programming patterns, and standard library methods.
It targets developers who know programming basics but need fast recall of Java-specific syntax without digging through the full Java Language Specification (JLS) or Oracle docs.
Java is not a language you learn once and never look up. Even senior developers regularly reference method signatures, type ranges, and collection syntax. Over 90% of Fortune 500 companies use Java for their software systems (Netguru, 2025), which means Java syntax is a long-term skill with real daily use.
Scope of this reference: Java SE (Standard Edition), which underpins Android development, back-end development, and most enterprise application stacks. It covers the core language, not Spring, Hibernate, or Android SDK specifics.
This cheat sheet pairs well with the python cheat sheet, javascript cheat sheet, and sql cheat sheet if you work across multiple languages.
What Are the Java Data Types and Variable Declarations?
Java data types define what kind of value a variable holds and how much memory the JVM allocates for it. Java provides 8 primitive types and a separate category of reference types including String, arrays, and objects.
Java 10 also introduced var for local type inference, so you can skip explicit declarations in many method bodies.
Primitive Types Table
|
Type |
Size |
Default |
Range |
|---|---|---|---|
|
|
1 byte |
0 |
-128 to 127 |
|
|
2 bytes |
0 |
-32,768 to 32,767 |
|
|
4 bytes |
0 |
-2,147,483,648 to 2,147,483,647 |
|
|
8 bytes |
0L |
-9.2 × 10^18 to 9.2 × 10^18 |
|
|
4 bytes |
0.0f |
~7 decimal digits precision |
|
|
8 bytes |
0.0d |
~15 decimal digits precision |
|
|
2 bytes |
|
0 to 65,535 (Unicode) |
|
|
1 byte |
false |
true / false |
Primitive types store values directly on the stack. A reference type (Integer, String, an object) stores a heap address instead.
Accessing a primitive int at 4 bytes is significantly faster than accessing its Integer wrapper at 16 bytes on a 64-bit JVM (codingpancake, 2026).
Reference Types and Null Handling
String is not a primitive. It is an immutable object in the java.lang package. String literals are stored in the JVM String pool; objects created with new String() are not.
String a = "hello"; // String pool
String b = new String("hello"); // heap object, avoid this
Null is the default value for any uninitialized reference type. Calling methods on a null reference throws NullPointerException. Java 17 and later provide improved NPE messages identifying which variable is null.
Arrays are reference types. int[] holds primitive values but the array itself lives on the heap.
int[] scores = new int[5]; // default values: 0
String[] names = {"Alice", "Bob"}; // inline init
var total = 100; // type inferred as int (Java 10+)
Type Casting Syntax
Widening (implicit): No data loss. Smaller type assigned to a larger type automatically.
int x = 42;
double d = x; // widening: int -> double, no cast needed
Narrowing (explicit): Possible data loss. Cast required.
double d = 9.99;
int x = (int) d; // x = 9, decimal truncated
final declares a constant. By convention, constants use UPPER_SNAKE_CASE.
final int MAX_RETRIES = 3;
What Are the Java Operators?
Java operators cover 6 categories: arithmetic, relational, logical, bitwise, assignment, and the ternary operator. Knowing operator precedence prevents logic bugs that are annoying to track down.
Arithmetic and Assignment
int a = 10, b = 3;
a + b // 13
a - b // 7
a * b // 30
a / b // 3 (integer division, no decimal)
a % b // 1 (modulus)
a++ // post-increment: use then add
++a // pre-increment: add then use
Compound assignment operators combine an operation with assignment:
a += 5; // a = a + 5
a -= 2; // a = a - 2
a *= 3; // a = a * 3
a /= 4; // a = a / 4
a %= 2; // a = a % 2
Relational and Logical
Relational operators return boolean:
a == b // equal
a != b // not equal
a > b // greater than
a < b // less than
a >= b // greater or equal
a <= b // less or equal
Logical operators:
&& // AND, both must be true
|| // OR, at least one true
! // NOT, inverts boolean
Short-circuit evaluation: && stops if the left side is false. || stops if the left side is true. This matters when the right side has side effects.
Bitwise and Ternary
Bitwise operators work at the binary bit level:
a & b // AND
a | b // OR
a ^ b // XOR
~a // bitwise complement
a << 2 // left shift (multiply by 4)
a >> 2 // right shift (divide by 4, signed)
a >>> 2 // unsigned right shift
Ternary operator: Compact conditional. One line, two outcomes.
String result = (a > b) ? "greater" : "not greater";
Precedence order (high to low, partial): ++/-- → *///% → +/- → <</>> → relational → ==/!= → & → ^ → | → && → || → ternary → assignment.
When in doubt, use parentheses. They override precedence and make intent explicit.
What Are the Java Control Flow Statements?
Control flow statements determine which code runs, how many times, and under what conditions. Java provides 3 branching constructs and 3 loop types, plus 3 control keywords.
Conditional Statements
if (score >= 90) {
System.out.println("A");
} else if (score >= 75) {
System.out.println("B");
} else {
System.out.println("C");
}
Classic switch statement:
switch (day) {
case "MON": System.out.println("Monday"); break;
case "FRI": System.out.println("Friday"); break;
default: System.out.println("Other");
}
Switch expression (Java 14+): No fall-through, no break needed. Returns a value directly.
String label = switch (day) {
case "MON", "TUE" -> "Weekday";
case "SAT", "SUN" -> "Weekend";
default -> "Other";
};
Switch expressions are cleaner. Use them over classic switch for new code on Java 14+.
Loop Types and Syntax
|
Loop |
Best For |
Key Trait |
|---|---|---|
|
|
Known iteration count |
Init, condition, update in one line |
|
|
Unknown count, check first |
May never execute |
|
|
Unknown count, run at least once |
Always executes body once |
|
|
Arrays, Iterable collections |
Cleaner, no index access |
// Standard for
for (int i = 0; i < 5; i++) { System.out.println(i); }
// While
int i = 0;
while (i < 5) { System.out.println(i); i++; }
// Do-while
do { System.out.println(i); i++; } while (i < 5);
// For-each
int[] nums = {1, 2, 3};
for (int n : nums) { System.out.println(n); }
Loop Control Keywords
break exits the loop immediately.
continue skips the current iteration and moves to the next.
Labeled break exits a specific outer loop when working with nested loops:
outer:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) break outer; // exits both loops
}
}
return exits the entire method, not just the loop.
What Are Java Arrays and Their Key Methods?
A Java array is a fixed-size, ordered collection of elements with the same type. Once created, its length cannot change. For dynamic sizing, use ArrayList instead.
The java.util.Arrays class provides utility methods that cover most common array operations.
Array Declaration and Initialization
// Static init
int[] scores = {85, 90, 78, 92};
// Dynamic init (values default to 0)
int[] scores = new int[4];
// 2D array
int[][] matrix = new int[3][4]; // 3 rows, 4 cols
int[][] grid = {{1,2}, {3,4}, {5,6}}; // inline
Access any element with its zero-based index: scores[0] returns the first element. Accessing an out-of-range index throws ArrayIndexOutOfBoundsException.
Array length uses the .length property, not a method: scores.length. Note that String.length() uses parentheses. This trips people up constantly.
Arrays Utility Methods
import java.util.Arrays;
int[] arr = {5, 2, 8, 1};
Arrays.sort(arr); // sorts in-place: [1, 2, 5, 8]
Arrays.toString(arr) // "[1, 2, 5, 8]", for printing
Arrays.copyOf(arr, 6) // copy, pad with 0s to length 6
Arrays.copyOfRange(arr, 1, 3) // elements at index 1 and 2
Arrays.fill(arr, 0) // set all elements to 0
Arrays.binarySearch(arr, 5) // index of 5 (array must be sorted)
Arrays.equals(arr, otherArr) // true if same length and values
Arrays.sort() runs in O(n log n) using dual-pivot Quicksort for primitives and TimSort for objects.
What Are the Java String Methods?
A Java String is an immutable sequence of characters backed by a char[] array internally. Every method that appears to "modify" a string actually creates and returns a new String object.
The java.lang.String class is automatically imported. No import statement needed.
Common String Methods Table
|
Method |
Returns |
Example |
|---|---|---|
|
|
int |
|
|
|
char |
|
|
|
String |
|
|
|
int |
|
|
|
boolean |
|
|
|
String |
|
|
|
String[] |
|
|
|
String |
|
|
|
String |
|
|
|
String |
|
|
|
boolean |
|
|
|
boolean |
|
String comparison: Always use .equals(), not ==. The == operator compares object references, not values. Two String objects with identical content can still fail == if they were not interned.
String a = new String("hello");
String b = new String("hello");
a == b // false, different heap objects
a.equals(b) // true, same characters
StringBuilder Syntax
Baeldung JMH benchmarks show StringBuilder is the fastest string-building method, outperforming String.format() and StringBuffer in single-threaded scenarios.
Use StringBuilder when concatenating strings inside loops. Using + inside a loop creates a new heap object on every iteration.
StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(", ");
sb.append("World");
String result = sb.toString(); // "Hello, World"
// Chaining
String r = new StringBuilder()
.append("Java")
.append(" 21")
.toString();
StringBuffer is the thread-safe version. Use it only when multiple threads write to the same buffer. For single-threaded code, StringBuilder is always faster.
String.format() is readable but slower due to internal regex parsing. Fine for low-frequency formatting; avoid it inside tight loops.
String msg = String.format("User: %s, Score: %d", name, score);
// printf equivalent:
System.out.printf("Name: %s%n", name);
What Are Java Classes, Objects, and Constructors?
A class is a blueprint. An object is an instance of that blueprint created at runtime with new. Every Java program with a main() method is a class.
JetBrains reports that 72% of Java developers use IntelliJ IDEA as their primary IDE, and the IDE's code generation features handle constructor scaffolding and getter/setter generation automatically. Still worth knowing the syntax by hand.
Class Declaration Syntax
public class Car {
// Fields (instance variables)
private String brand;
private int year;
private static int count = 0; // shared across all instances
// Default constructor
public Car() {
count++;
}
// Parameterized constructor
public Car(String brand, int year) {
this.brand = brand; // 'this' disambiguates field from param
this.year = year;
count++;
}
// Copy constructor
public Car(Car other) {
this.brand = other.brand;
this.year = other.year;
}
// Static method
public static int getCount() { return count; }
// Instance method
public String getBrand() { return brand; }
public void setBrand(String brand) { this.brand = brand; }
}
Object Instantiation
Car c1 = new Car("Toyota", 2022);
Car c2 = new Car(c1); // copy constructor
Car c3 = new Car(); // default constructor
System.out.println(Car.getCount()); // 3, static method on class
System.out.println(c1.getBrand()); // "Toyota", instance method
Static vs Instance Members
|
Member Type |
Belongs To |
Access |
Use Case |
|---|---|---|---|
|
Instance field |
Each object |
|
Per-object state |
|
Static field |
The class |
|
Shared counters, constants |
|
Instance method |
Each object |
|
Object behavior |
|
Static method |
The class |
|
Utility, factory methods |
this refers to the current object instance. It resolves naming conflicts between constructor parameters and instance fields. It also passes the current object to another method.
If a constructor has no explicit call to another constructor or to super(), the compiler inserts a call to super() automatically.
What Are the Java OOP Concepts With Syntax Examples?
Java's object-oriented model rests on 4 pillars: inheritance, encapsulation, polymorphism, and abstraction. These are not theoretical concepts in Java. They map directly to keywords and syntax patterns you write every day.
Spring, the most widely used Java framework, is used by 65% of Java developers (JetBrains, 2025). Every Spring bean and service class relies on these OOP principles, especially encapsulation and interface-based abstraction.
Inheritance and Super Keyword
Inheritance uses extends. A subclass inherits all public and protected members from its parent.
class Animal {
String name;
void speak() { System.out.println("..."); }
}
class Dog extends Animal {
@Override
void speak() {
super.speak(); // calls parent version
System.out.println("Woof!");
}
}
Key rules:
-
Java supports single class inheritance only (one
extendsper class) -
super.methodName()calls the overridden parent version -
@Overrideannotation is not required but catches typos at compile time
Interfaces vs. Abstract Classes
Both define contracts. The choice matters for design.
|
Feature |
Abstract Class |
Interface |
|---|---|---|
|
Instantiation |
No |
No |
|
Constructor |
Yes |
No |
|
Fields |
Any type |
|
|
Default methods |
Yes |
Yes (Java 8+) |
|
Multiple inheritance |
No |
Yes ( |
|
When to use |
Shared state + behavior |
Pure contract / multiple types |
interface Drawable {
void draw(); // abstract by default
default void reset() { } // default implementation (Java 8+)
}
abstract class Shape {
String color;
abstract double area(); // subclasses must implement
}
class Circle extends Shape implements Drawable {
double radius;
@Override public double area() { return Math.PI * radius * radius; }
@Override public void draw() { System.out.println("Drawing circle"); }
}
Method Overloading vs. Overriding
Two concepts. Completely different mechanics.
Overloading (compile-time polymorphism): Same method name, different parameter list. Resolved at compile time.
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
String add(String a, String b) { return a + b; }
Overriding (runtime polymorphism): Subclass provides its own version of a parent method. Resolved at runtime based on the actual object type.
Animal a = new Dog(); // reference is Animal, object is Dog
a.speak(); // calls Dog's speak(), not Animal's
The actual object type, not the reference type, determines which overridden method runs. That is the core of runtime polymorphism in the Java virtual machine.
What Are the Java Access Modifiers?
Java has 4 access modifiers that control where classes, methods, and fields are visible. Getting this wrong breaks encapsulation and makes codebases hard to maintain.
JetBrains survey data shows 68% of Java developers prefer private access for fields to minimize unintended interactions.
Access Modifier Scope Table
|
Modifier |
Same Class |
Same Package |
Subclass |
Anywhere |
|---|---|---|---|---|
|
|
Yes |
No |
No |
No |
|
default (none) |
Yes |
Yes |
No |
No |
|
|
Yes |
Yes |
Yes |
No |
|
|
Yes |
Yes |
Yes |
Yes |
Design rule: start with private. Only widen visibility when there is a specific reason to do so.
Non-Access Modifiers
Non-access modifiers change class and member behavior without affecting visibility.
static: Belongs to the class, not an instance. No object needed to call it.
public static int count = 0; // class-level field
public static void printCount() { } // class-level method
final: 3 different behaviors depending on context:
final int MAX = 100; // constant, value cannot change
final class ImmutableClass { } // cannot be subclassed
final void criticalMethod() { } // cannot be overridden
abstract: Class cannot be instantiated. Method has no body and must be implemented by a concrete subclass.
synchronized: Method or block allows only one thread at a time. Used in multi-threaded programming.
What Are the Java Collections Framework Classes?
The Java Collections Framework (JCF) is part of java.util and provides interfaces (List, Set, Map, Queue) with concrete implementations. Research on real-world Java codebases shows ArrayList accounts for 47% of all collection instantiations, followed by HashMap at 23% (SMU empirical study).
List Implementations
Declare to the interface, not the implementation. List<String> list = new ArrayList<>() is correct. ArrayList<String> list = new ArrayList<>() is fine but couples your code to the implementation.
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.get(0); // "Alice"
names.remove("Bob");
names.size(); // 1
names.contains("Alice"); // true
Collections.sort(names);
ArrayList gives O(1) random access. LinkedList is faster for mid-list insertions but slower for indexed access due to cache misses. Use ArrayList by default.
Map Implementations
HashMap is the go-to for key-value storage. O(1) average for get and put.
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 95);
scores.put("Bob", 88);
scores.get("Alice"); // 95
scores.containsKey("Bob"); // true
scores.remove("Bob");
// Iterating entries
for (Map.Entry<String, Integer> e : scores.entrySet()) {
System.out.println(e.getKey() + ": " + e.getValue());
}
LinkedHashMap preserves insertion order. TreeMap sorts by key. Use TreeMap when you need ordered keys or range queries (subMap, headMap).
Set and Queue Types
HashSet, unique elements, no guaranteed order:
Set<String> ids = new HashSet<>();
ids.add("A1"); ids.add("A2"); ids.add("A1"); // duplicate ignored
ids.size(); // 2
Queue and Deque: ArrayDeque replaces both Stack and LinkedList as the preferred queue implementation.
Deque<String> queue = new ArrayDeque<>();
queue.offer("task1"); // add to tail
queue.poll(); // remove from head
queue.push("task2"); // add to head (stack behavior)
queue.pop(); // remove from head (stack behavior)
Avoid Stack (legacy, synchronized). Avoid Vector (legacy, synchronized). Both have better modern replacements.
What Are Java Exception Handling Keywords and Patterns?
Exception handling separates error-management code from business logic. Java uses a checked/unchecked model that forces you to handle recoverable errors at compile time.
try-catch-finally is the foundation. The finally block runs regardless of whether an exception was thrown. Good for cleanup.
try-catch-finally Block Structure
try {
int result = 10 / 0; // throws ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Math error: " + e.getMessage());
} catch (Exception e) {
System.out.println("Other error"); // more specific catches go first
} finally {
System.out.println("Always runs"); // cleanup here
}
Multi-catch (Java 7+): Handle multiple exception types in one block.
catch (IOException | SQLException e) {
System.out.println("IO or DB error: " + e.getMessage());
}
Checked vs. Unchecked Exceptions
|
Type |
Examples |
Must Handle? |
Cause |
|---|---|---|---|
|
Checked |
|
Yes |
External, recoverable |
|
Unchecked |
|
No |
Programming errors |
|
Error |
|
No |
JVM-level, unrecoverable |
Oracle's rule: if a caller can reasonably recover from it, make it checked. If not, use unchecked.
Try-With-Resources and Custom Exceptions
Try-with-resources (Java 7+): Auto-closes any AutoCloseable resource when the block exits.
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
} // br.close() called automatically, no finally needed
Custom exception:
class InsufficientFundsException extends RuntimeException {
private double amount;
public InsufficientFundsException(double amount) {
super("Insufficient funds: " + amount);
this.amount = amount;
}
}
Extend Exception for checked, RuntimeException for unchecked. Never leave catch blocks empty. Always log or rethrow.
What Are Java Lambda Expressions and Functional Interfaces?
Lambda expressions, introduced in Java 8, allow passing behavior as data. They reduce boilerplate by replacing anonymous inner classes with concise inline functions.
Lambda syntax:
// No params
Runnable r = () -> System.out.println("Running");
// One param (parens optional)
Consumer<String> print = s -> System.out.println(s);
// Multiple params, multiple statements
Comparator<Integer> cmp = (a, b) -> {
int result = Integer.compare(a, b);
return result;
};
Core Functional Interfaces from java.util.function
Java 8 introduced 4 primary functional interface types in java.util.function:
Predicate<T>: Takes T, returns boolean. Used for filtering.
Predicate<String> isLong = s -> s.length() > 5;
isLong.test("Hello"); // false
isLong.test("HelloWorld"); // true
Function<T,R>: Takes T, returns R. Used for mapping/transformation.
Function<String, Integer> strLen = s -> s.length();
strLen.apply("Hello"); // 5
Consumer<T>: Takes T, returns nothing. Used for side effects.
Consumer<String> logger = s -> System.out.println("LOG: " + s);
logger.accept("Event fired");
Supplier<T>: Takes nothing, returns T. Used for lazy initialization.
Supplier<Double> random = () -> Math.random();
random.get(); // returns a random double
Method References and @FunctionalInterface
Method references are shorthand for lambdas that just call a method.
// static method reference
Function<String, Integer> parse = Integer::parseInt;
// instance method reference
Consumer<String> print = System.out::println;
// constructor reference
Supplier<ArrayList<String>> listFactory = ArrayList::new;
@FunctionalInterface annotation is optional but recommended. It causes a compile error if someone accidentally adds a second abstract method to the interface.
What Are Java Stream API Methods?
The Stream API, also from Java 8, processes collections declaratively. A stream is a pipeline: data flows through a sequence of operations. Streams do not modify the source collection.
64% of enterprises use Java in cloud environments (Second Talent, 2025). Stream pipelines appear throughout modern Java cloud service code for filtering, transforming, and aggregating data.
Creating and Chaining Streams
List<String> names = List.of("Alice", "Bob", "Charlie", "David");
// From a collection
names.stream()
// From values
Stream.of("a", "b", "c")
// From an array
Arrays.stream(new int[]{1, 2, 3})
Intermediate operations (lazy, return a new Stream):
.filter(s -> s.startsWith("A")) // keep matching elements
.map(String::toUpperCase) // transform each element
.flatMap(s -> Arrays.stream(s.split(""))) // flatten nested streams
.sorted() // natural sort
.sorted(Comparator.reverseOrder()) // custom sort
.distinct() // remove duplicates
.limit(3) // take first 3
.skip(2) // skip first 2
Terminal operations (eager, consume the stream):
.collect(Collectors.toList()) // to List
.collect(Collectors.toSet()) // to Set
.collect(Collectors.joining(", ")) // concatenate strings
.collect(Collectors.groupingBy(String::length)) // group by key
.forEach(System.out::println) // side effect
.count() // element count
.findFirst() // Optional<T>
.reduce(0, Integer::sum) // fold to single value
Full Pipeline Example
List<String> result = names.stream()
.filter(n -> n.length() > 3)
.map(String::toLowerCase)
.sorted()
.collect(Collectors.toList());
// ["alice", "charlie", "david"]
Parallel streams: Add .parallelStream() instead of .stream() to split work across CPU cores.
long count = names.parallelStream()
.filter(n -> n.contains("a"))
.count();
Parallel streams help with large datasets and CPU-bound operations. Avoid them for small collections or I/O-bound work. The overhead of splitting and merging outweighs the benefit for most small collections.
What Are Java File I/O and Key java.nio Methods?
Java provides 2 file I/O APIs: the legacy java.io (stream-based, blocking) and the modern java.nio.file (buffer-based, richer). For most file tasks today, java.nio.file.Files is the cleaner choice.
java.nio was introduced in Java 1.4. java.nio.file (NIO.2) arrived in Java 7, adding the Path, Files, and Paths classes used in all modern Java file code (Baeldung, 2024).
Reading Files with java.nio
import java.nio.file.*;
import java.io.IOException;
Path path = Path.of("data.txt"); // Java 11+
// Path path = Paths.get("data.txt"); // Java 7-10
// Read entire file as String (Java 11+)
String content = Files.readString(path);
// Read all lines into a List
List<String> lines = Files.readAllLines(path);
// Read lines as a Stream (memory-efficient for large files)
try (Stream<String> stream = Files.lines(path)) {
stream.forEach(System.out::println);
}
Files.lines() processes lazily. It does not load the entire file into memory at once. Correct for large log files or CSV imports.
Writing Files with java.nio
// Write a String (creates or overwrites)
Files.writeString(path, "Hello, Java!");
// Write lines
List<String> data = List.of("line1", "line2");
Files.write(path, data);
// Append to existing file
Files.writeString(path, "\nNew line",
StandardOpenOption.APPEND);
BufferedReader and BufferedWriter (java.io)
Use BufferedReader/BufferedWriter from java.io when working with legacy code or when you need line-by-line processing with more control.
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
try (BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt", true))) {
bw.write("Appended line");
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
}
FileWriter(path, true) sets append mode. Without true, the file is overwritten.
Always wrap file operations in try-with-resources. Every file handle is AutoCloseable. Leaving one open is a resource leak that causes production issues.
For more on related reference sheets, see the git cheat sheet, docker cheat sheet, bash cheat sheet, and regex cheat sheet if you work across command-line and scripting tools alongside Java.
If you are evaluating which IDE to use for Java development, comparisons like Kotlin or Java and tools like Android Studio or IntelliJ IDEA vs Android Studio provide context on the broader software development toolchain Java fits into.
FAQ on Java Cheat Sheet
What is the difference between == and .equals() in Java?
== compares object references, not values. Two String objects with identical content can return false with == if they live on different heap addresses. Always use .equals() for string comparison in Java.
What are the 8 primitive data types in Java?
They are byte, short, int, long, float, double, char, and boolean. Each has a fixed size. int takes 4 bytes, long takes 8. These are the only types that store values directly on the stack rather than as heap references.
What is the difference between ArrayList and LinkedList?
ArrayList uses a resizable array. Random access is O(1). LinkedList uses a doubly-linked structure with faster mid-list insertions but slower indexed access. In practice, ArrayList outperforms LinkedList in most real-world scenarios due to CPU cache efficiency.
When should I use StringBuilder instead of String concatenation?
Use StringBuilder inside loops. The + operator creates a new immutable String object on every iteration, producing unnecessary heap objects. For simple one-off concatenations outside loops, the + operator is fine and readable.
What is the difference between checked and unchecked exceptions?
Checked exceptions like IOException are verified at compile time and must be handled or declared. Unchecked exceptions like NullPointerException are runtime errors. If a caller can reasonably recover from a failure, use a checked exception. Otherwise, use unchecked.
What does the static keyword do in Java?
static binds a field or method to the class itself, not to any instance. You call it as ClassName.method() without creating an object. Static fields are shared across all instances, making them useful for counters, constants, and utility methods.
What is the difference between an interface and an abstract class?
An abstract class can hold state, constructors, and concrete methods. An interface defines a pure contract. A class can implement multiple interfaces but only extend one abstract class. Use interfaces when you need multiple inheritance of behavior across unrelated types.
How does the Java Stream API differ from a for loop?
Streams are declarative. You describe what to do, not how to iterate. A stream().filter().map().collect() pipeline is often more readable than a nested loop. Streams also support parallel execution via .parallelStream() with minimal code changes.
What is try-with-resources and when should I use it?
It automatically closes any AutoCloseable resource when the block exits, eliminating the need for a finally block. Use it whenever you open a file, database connection, or network socket. It prevents resource leaks that cause hard-to-trace production failures.
What is the difference between HashMap, LinkedHashMap, and TreeMap?
HashMap offers O(1) lookups with no ordering guarantee. LinkedHashMap preserves insertion order. TreeMap sorts keys by natural order or a custom Comparator, at the cost of O(log n) operations. Choose based on whether ordering matters for your use case.