Java Cheat Sheet
Core Java syntax, data types, collections, generics, and streams for writing robust object-oriented JVM applications.
2 PagesBeginnerApr 15, 2026
Basic Syntax
Variables, control flow, and loops.
java
public class Main { public static void main(String[] args) { int age = 30; // primitive int String name = "Ada"; // reference type double pi = 3.14159; boolean isJavaFun = true; if (age >= 18) { System.out.println(name + " is an adult"); } else { System.out.println(name + " is a minor"); } for (int i = 0; i < 5; i++) { System.out.println("Count: " + i); } }}
Collections Framework
Lists, maps, and sets from java.util.
java
List<String> names = new ArrayList<>();names.add("Alice");names.add("Bob");names.get(0); // "Alice"names.remove("Bob");Map<String, Integer> ages = new HashMap<>();ages.put("Alice", 30);ages.getOrDefault("Bob", 0); // 0ages.containsKey("Alice"); // trueSet<Integer> unique = new HashSet<>(List.of(1, 2, 2, 3)); // {1, 2, 3}
Streams API
Functional-style collection processing.
java
List<Integer> nums = List.of(1, 2, 3, 4, 5, 6);List<Integer> evens = nums.stream() .filter(n -> n % 2 == 0) .map(n -> n * n) .collect(Collectors.toList()); // [4, 16, 36]int sum = nums.stream().mapToInt(Integer::intValue).sum(); // 21Optional<Integer> max = nums.stream().max(Integer::compareTo);
OOP Keywords
Core keywords for object-oriented Java.
- extends- inherits from a superclass (single inheritance)
- implements- implements one or more interfaces
- @Override- annotation marking a method that overrides a superclass/interface method
- abstract- declares a class or method with no full implementation
- final- prevents further subclassing, overriding, or reassignment
- static- belongs to the class rather than an instance
- interface- defines a contract of methods a class must implement
- try/catch/finally- exception handling; finally always runs
Exception Handling
try-catch-finally and try-with-resources.
java
// try-with-resources auto-closes AutoCloseabletry (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); }} catch (IOException e) { System.err.println("Error: " + e.getMessage());} finally { System.out.println("Done");}// Custom exceptionclass InvalidAgeException extends Exception { InvalidAgeException(String msg) { super(msg); }}
Records & Sealed Classes
Modern immutable data carriers and restricted hierarchies.
java
// Record: immutable, auto equals/hashCode/toStringpublic record Point(int x, int y) { Point { if (x < 0 || y < 0) throw new IllegalArgumentException(); }}// Sealed interface limits which types can implement itpublic sealed interface Shape permits Circle, Square {}public record Circle(double r) implements Shape {}public record Square(double s) implements Shape {}
Generics
Type-safe classes and methods with bounded wildcards.
java
// Generic methodpublic static <T extends Comparable<T>> T max(List<T> list) { T best = list.get(0); for (T item : list) { if (item.compareTo(best) > 0) best = item; } return best;}// Bounded wildcard: accepts List of Number or any subtypestatic double sum(List<? extends Number> nums) { double total = 0; for (Number n : nums) total += n.doubleValue(); return total;}
Access & Non-Access Modifiers
Visibility and behavior modifiers for members.
- public- accessible from any other class
- private- accessible only within the declaring class
- protected- accessible within the package and by subclasses
- (default)- package-private: accessible only within the same package
- static- belongs to the class rather than any instance
- final- constant value, non-overridable method, or non-subclassable class
- abstract- declared without an implementation; must be subclassed
- synchronized- only one thread may execute the method at a time
Pro Tip
Prefer List.of() and Map.of() for immutable collections in modern Java (9+) instead of manually wrapping with Collections.unmodifiableList().
Was this cheat sheet helpful?
Explore Topics
#Java#JavaCheatSheet#Programming#Beginner#BasicSyntax#CollectionsFramework#StreamsAPI#OOPKeywords#OOP#DataStructures#CheatSheet#SkillVeris