AI Generate Java docs instantly

Java Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

5 snippets

Your first steps with Java basics

Hello World

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

Comments

// Single line comment
/* Multi-line
   comment */
/** Javadoc comment */

User Input

Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();

Type Conversion

Integer.parseInt("42")  // String to int
String.valueOf(42)      // int to String
Double.parseDouble("3.14")

Check Type

obj instanceof String  // true/false
obj.getClass().getName()

Variables & Data Types

8 snippets

Declaring and using different data types

String

String name = "John";

Integer

int age = 25;

Double

double price = 19.99;

Boolean

boolean isActive = true;

Array

int[] nums = {1, 2, 3, 4, 5};

ArrayList

ArrayList<String> list = new ArrayList<>();

HashMap

HashMap<String, Integer> map = new HashMap<>();

Final

final double PI = 3.14159;

Operators

7 snippets

Symbols for calculations and comparisons

Arithmetic

5 + 3   // 8 (addition)
5 - 3   // 2 (subtraction)
5 * 3   // 15 (multiplication)
5 / 3   // 1 (integer division)
5 % 3   // 2 (modulus)
int x = 5; x++  // 6 (increment)

Comparison

5 == 5  // true (equal)
5 != 3  // true (not equal)
5 > 3   // true (greater)
5 < 3   // false (less)
5 >= 5  // true
"a".equals("a")  // true (strings)

Logical

true && false   // false (AND)
true || false   // true (OR)
!true           // false (NOT)

// Short-circuit: right side not evaluated
x != 0 && 10/x > 1

Assignment

int x = 5    // Assign
x += 3       // x = 8 (add)
x -= 2       // x = 6 (subtract)
x *= 2       // x = 12 (multiply)
x /= 3       // x = 4 (divide)
x %= 3       // x = 1 (modulus)

Ternary

int a = 10, b = 5;
int max = (a > b) ? a : b;  // max = 10

Instanceof

Object obj = "Hello";
if (obj instanceof String s) {
    System.out.println(s.length());  // 5
}

Bitwise

5 & 3   // 1 (AND: 101 & 011)
5 | 3   // 7 (OR: 101 | 011)
5 ^ 3   // 6 (XOR)
~5      // -6 (NOT)
5 << 1  // 10 (left shift)
5 >> 1  // 2 (right shift)

Tired of looking up syntax?

DocuWriter.ai generates documentation and explains code using AI.

Try Free

Loops

10 snippets

Repeating code blocks efficiently

For

for (int i = 0; i < 5; i++) {
    System.out.println(i);
}

Enhanced For

for (String item : list) {
    System.out.println(item);
}

While

while (count < 10) {
    count++;
}

Do While

do {
    count++;
} while (count < 10);

Break

for (int i = 0; i < 10; i++) {
    if (i == 5) break;
}

Continue

for (int i = 0; i < 10; i++) {
    if (i == 5) continue;
    System.out.println(i);
}

Labeled Break

outer: for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (j == 1) break outer;
    }
}

forEach (Streams)

list.forEach(item -> {
    System.out.println(item);
});

Iterator

Iterator<String> it = list.iterator();
while (it.hasNext()) {
    System.out.println(it.next());
}

Map Iteration

map.forEach((key, value) -> {
    System.out.println(key + ": " + value);
});

Conditionals

5 snippets

Making decisions in your code

If/Else

if (x > 0) {
    System.out.println("positive");
} else if (x < 0) {
    System.out.println("negative");
} else {
    System.out.println("zero");
}

Switch Expression

String result = switch (day) {
    case "Mon" -> "Monday";
    case "Tue" -> "Tuesday";
    default -> "Other";
};

Switch Statement

switch (day) {
    case "Mon":
        System.out.println("Monday");
        break;
    default:
        System.out.println("Other");
}

Pattern Matching

if (obj instanceof String s && s.length() > 5) {
    System.out.println(s);
}

Ternary

String msg = (age >= 18) ? "adult" : "minor";

Methods

4 snippets

Method

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

Static Method

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

Void Method

public void printMessage(String msg) {
    System.out.println(msg);
}

Varargs

public int sum(int... nums) {
    return Arrays.stream(nums).sum();
}

Classes & OOP

4 snippets

Class

public class Dog {
    private String name;

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

    public String bark() {
        return name + " says woof!";
    }
}

Inheritance

public class Puppy extends Dog {
    public Puppy(String name) {
        super(name);
    }
}

Interface

public interface Animal {
    void speak();
}

Record

public record Person(String name, int age) {}

Exception Handling

3 snippets

Try/Catch

try {
    int result = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Error: " + e.getMessage());
}

Finally

try {
    riskyOperation();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    cleanup();
}

Throw

throw new IllegalArgumentException("Invalid input");

Streams API

4 snippets

Filter

list.stream().filter(x -> x > 0).toList();

Map

list.stream().map(String::toUpperCase).toList();

Reduce

nums.stream().reduce(0, Integer::sum);

Collect

stream.collect(Collectors.toList());

More Cheat Sheets

FAQ

Frequently asked questions

What is a Java cheat sheet?

A Java cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Java. It helps developers quickly look up syntax without searching through documentation.

How do I learn Java quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Java concepts?

Key Java concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Java code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Java code using AI.

Code Conversion Tools

Convert Java to Other Languages

Easily translate your Java code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate Java Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week