About Us Contact Us Write for Us Advertise
Home > Java > Java Cheat Sheet: Essential Syntax Quick Reference
Java

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.

Shiv Pandey
Shiv Pandey
Oct 17, 2026 | 91 views
Java Cheat Sheet: Essential Syntax Quick Reference

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+)

→ Variables & data types

Operators

+ - * / %          // arithmetic (% = remainder)
== != < > <= >=     // comparison (use .equals() for objects!)
&& || !            // logical AND, OR, NOT
+= -= *= /=        // compound assignment

→ Operators

Control flow

if (age >= 18) { ... } else { ... }

switch (day) {
    case 1 -> System.out.println("Mon");
    default -> System.out.println("Other");
}

→ if/else & switch

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

→ Loops

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);
}

→ Methods

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)

→ Arrays · → 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();

→ Classes & objects

Exception handling

try {
    risky();
} catch (Exception e) {
    System.out.println("Error: " + e.getMessage());
} finally {
    cleanup();   // always runs
}

→ Exception handling

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.

Related

← Core Java course · Collections cheat sheet →

Related Articles

Java Collections Cheat Sheet: Which to Use & How
Java

Java Collections Cheat Sheet: Which to Use & How

Common Java Errors and How to Fix Them (Beginner Cheat Sheet)
Java

Common Java Errors and How to Fix Them (Beginner Cheat Sheet)

Java Syntax Basics: Your First Program Explained Line by Line
Java

Java Syntax Basics: Your First Program Explained Line by Line

Advanced Java Streams Interview Questions (Senior-Level, With Code)
Java

Advanced Java Streams Interview Questions (Senior-Level, With Code)