Java Cheat Sheet
JVM & Compilation Model
- Java code is compiled into bytecode by
javac
and executed on the JVM. - Strongly typed, statically compiled language with runtime polymorphism.
javac Main.java
java Main
Class Structure
public class MyClass {
private final int value;
public MyClass(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
Access Modifiers
private
: accessible only within the classprotected
: within the package and subclassespublic
: accessible from everywhere- package-private (default): accessible within the same package
Main Method
public class App {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
Primitive Types
int x = 42;
double d = 3.14;
char c = 'a';
boolean flag = true;
Objects vs primitives: use wrapper classes (Integer
, Double
, etc.) for collections and generics.
Collections Framework
import java.util.*;
List<String> list = new ArrayList<>();
list.add("hello");
Set<Integer> set = new HashSet<>();
Map<String, Integer> map = new HashMap<>();
Common Iteration
for (String s : list) {
System.out.println(s);
}
list.forEach(System.out::println);
Generics
public class Box<T> {
private T value;
public void set(T value) { this.value = value; }
public T get() { return value; }
}
Bounded types:
<T extends Number> void print(T t) { ... }
Interfaces & Abstract Classes
public interface Drawable {
void draw();
}
public abstract class Shape {
abstract void draw();
}
Java 8+ interfaces can have default
and static
methods.
Lambdas & Functional Interfaces (Java 8+)
List<Integer> list = List.of(1, 2, 3);
list.forEach(x -> System.out.println(x));
Functional interface example:
@FunctionalInterface
interface Operation {
int apply(int x, int y);
}
Streams API (Java 8+)
List<String> names = List.of("Rafa", "Marie", "Scala");
List<String> filtered = names.stream()
.filter(s -> s.length() > 4)
.map(String::toUpperCase)
.sorted()
.toList();
Terminal ops: forEach
, collect
, reduce
, count
Intermediate ops: filter
, map
, flatMap
, distinct
Exceptions
try {
riskyOp();
} catch (IOException e) {
e.printStackTrace();
} finally {
cleanup();
}
Custom exception:
public class MyException extends Exception {
public MyException(String msg) { super(msg); }
}
Records (Java 16+)
Concise immutable data classes:
public record User(String name, int age) {}
Automatically generates constructor, getters, equals
, hashCode
, toString
.
Enums
public enum Status {
PENDING, IN_PROGRESS, DONE
}
Enums can contain fields, constructors, and methods.
Threads & Concurrency
new Thread(() -> System.out.println("Running")).start();
Executor Service
ExecutorService executor = Executors.newFixedThreadPool(4);
executor.submit(() -> doWork());
executor.shutdown();
Annotations
@Override
public String toString() {
return "value";
}
Custom annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyMarker { }
Module System (Java 9+)
module my.module {
exports com.my.package;
requires other.module;
}
Useful for strong encapsulation in large systems.
Best Practices
- Favor composition over inheritance.
- Use
final
where possible. - Prefer interfaces over concrete classes for APIs.
- Use immutability and
Optional<T>
to model absence. - Avoid
null
unless working with legacy APIs. - Use
var
for local inference (Java 10+). - Don’t overuse checked exceptions.
Want to add: JUnit, Maven/Gradle, Spring basics, or annotations like @Autowired
, @Bean
? Let me know.