How to use the Java Cheat Sheet
- 1. Search or browseType a word like the function or task you need, or pick a section.
- 2. CopyEach snippet has its own Copy button, so you can paste it straight into your editor.
- 3. DownloadSave the whole Java cheat sheet as plain text or Markdown for offline revision.
What's in it
It covers modern Java (17 and later), including switch expressions, records and pattern matching for instanceof. The 40 snippets are organised into 10 sections: Basics, Strings, Arrays, Control flow, OOP, Exceptions, Collections, Streams, Input/Output, Tools.
Example
Search "string" to see why s.equals(t) is used instead of ==, and how new StringBuilder(s).reverse() reverses text. In the Streams section, IntStream.rangeClosed(1, 5).sum() gives 15, and Collectors.groupingBy(String::length) groups words by length.
Tips for using it
- Pair it with the Java practice problems; look up syntax here while you solve them.
- Learn the difference between List.of (immutable) and new ArrayList<>(List.of(…)) (mutable).
A quick look, one snippet per section
- Basics:
public static void main(String[] args) { }(program entry point) - Strings:
s.length(), s.charAt(0), s.substring(1, 4)(length, a character, part of a string) - Arrays:
int[] a = {5, 2, 8}; Arrays.sort(a);(sort an array) - Control flow:
for (int i = 0; i < n; i++) { }(counting loop) - OOP:
class Car extends Vehicle implements Comparable<Car> { }(inherit and implement) - Exceptions:
try { ... } catch (IOException e) { ... } finally { ... }(handle exceptions) - Collections:
List<Integer> l = new ArrayList<>(List.of(3, 1, 2));(mutable list from values) - Streams:
l.stream().filter(x -> x > 1).map(x -> x * 2).toList();(filter and transform (toList in Java 16+)) - Input/Output:
Scanner sc = new Scanner(System.in); int n = sc.nextInt();(read input)
Search the box above for the rest of the Java snippets, or download the full sheet.
Frequently asked questions
Which Java version?
Java 17 LTS and later; a few snippets note when a feature arrived.
Is there a separate collections sheet?
Yes. The Java Collections cheat sheet goes deeper.
Is the Java cheat sheet free?
Yes. You can search, read and copy every snippet in the Java cheat sheet without signing up. The first time you download it, we ask once for your name and WhatsApp number; after that downloads and every other Uncodemy tool open straight away.