☕ Java Cheatsheet

📌 Java reference✍️ Written by Mark Sullivan📅 Reviewed 2026-04-21⏱ ~5 min read

A one-page reference for the Java syntax you'll meet first. Press Ctrl + P to save as PDF — the print stylesheet hides everything except the code.

Hello, World

java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

Variables & Types

java
int n = 42;
long big = 9_000_000_000L;
double pi = 3.14;
String s = "hello";
boolean b = true;
final int MAX = 100;     // immutable

var x = 42;              // type inferred (Java 10+)

Strings

java
String s = "Aisha";
s.length();
s.toUpperCase();
s.contains("is");
s.replace("A", "B");
s.split(",");
String.join(",", new String[]{"a","b"});
s.equals("hi");          // CONTENT compare
// s == "hi" compares references — usually wrong

Arrays

java
int[] xs = {1, 2, 3};
xs.length;               // property, no parens
xs[0]; xs[xs.length-1];
Arrays.sort(xs);
int[] copy = xs.clone();

Lists (Resizable)

java
List<Integer> list = new ArrayList<>();
list.add(1);
list.remove(0);
list.set(0, 99);
list.size();
list.contains(1);
for (int n : list) { ... }

Maps

java
Map<String, Integer> m = new HashMap<>();
m.put("x", 1);
m.get("x");
m.containsKey("x");
m.remove("x");
for (var e : m.entrySet()) {
    e.getKey(); e.getValue();
}

Conditionals

java
if (cond) { ... } else if (cond2) { ... } else { ... }

// Ternary
String status = age >= 18 ? "adult" : "minor";

switch (day) {
    case "Sat":
    case "Sun": System.out.println("weekend"); break;
    default:    System.out.println("weekday");
}

Loops

java
for (int i = 0; i < 5; i++) { ... }
for (int x : xs) { ... }
while (cond) { ... }
do { ... } while (cond);

Methods

java
public static int add(int a, int b) {
    return a + b;
}

public static void main(String[] args) {
    int sum = add(2, 3);
}

Classes

java
public class User {
    private String name;

    public User(String name) { this.name = name; }

    public String getName() { return name; }

    public String greet() { return "Hi, " + name; }
}

User u = new User("Aisha");
📌
Need the concepts?

This is a syntax reference, not a tutorial. Each row maps to a concept lesson — start with the 13 concepts if anything here is unfamiliar.

M
Mark Sullivan
Lead writer · 8 yrs full-stack

Mark started coding in 2017 after switching from financial analysis. She's built production systems in Python (Django) and JavaScript (Node + React) at two startups, and has taught intro programming at his local community college since 2022. He owns the curriculum for variables, functions, conditionals, and loops on this site. More about Mark →