Java Cheat Sheet: Essential Syntax Quick Reference
A quick-reference Java cheat sheet covering everyday syntax: variables, operators, control flow, loops, methods, arrays, strings, classes, exceptions, and modern shortcuts.
Keep this page bookmarked — it's a quick-reference Java cheat sheet covering the syntax you use every day, all in one place. Whether you're mid-project and forgot the exact syntax, or revising before an interview, this is your fast lookup for Java essentials. Each section links to a full lesson if you want the deep dive.
Variables & data types
int age = 25; // whole number double price = 9.99; // decimal boolean active = true; // true/false char grade = 'A'; // single character String name = "Alice"; // text (an object) var count = 10; // type inferred (Java 10+)
Operators
+ - * / % // arithmetic (% = remainder) == != < > <= >= // comparison (use .equals() for objects!) && || ! // logical AND, OR, NOT += -= *= /= // compound assignment
Control flow
if (age >= 18) { ... } else { ... } switch (day) { case 1 -> System.out.println("Mon"); default -> System.out.println("Other"); }
Loops
for (int i = 0; i < 5; i++) { ... } // standard for for (String s : list) { ... } // enhanced for-each while (condition) { ... } // while do { ... } while (condition); // runs at least once
Methods
public int add(int a, int b) { // returns an int return a + b; } public void greet(String name) { // returns nothing System.out.println("Hi " + name); }
Arrays & Strings
int[] nums = {1, 2, 3}; nums[0]; // 1 nums.length; // 3 String s = "hello"; s.length(); // 5 s.charAt(0); // 'h' s.substring(1, 3); // "el" s.toUpperCase(); // "HELLO" (s unchanged — immutable!) s.equals("hello"); // true (never use == for strings)
Classes & objects
class Dog { private String name; Dog(String name) { this.name = name; } // constructor public void bark() { System.out.println("Woof!"); } } Dog d = new Dog("Rex"); // create an object d.bark();
Exception handling
try { risky(); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } finally { cleanup(); // always runs }
Modern shortcuts
// lambda Runnable r = () -> System.out.println("Hi"); // stream (filter + collect) list.stream().filter(x -> x > 5).toList(); // record (data class in one line) record Point(int x, int y) { }
→ Lambdas · → Streams · → Records
I still keep a cheat sheet like this open in a browser tab — even after years, nobody memorises every method signature, and there's no shame in looking things up (professional developers do it constantly). What actually matters is understanding the concepts; the exact syntax you can always refresh in seconds. So bookmark this page for those "wait, what's the substring syntax again?" moments, and lean on it freely while you build. For the collections-specific reference, see the Java collections cheat sheet.