Free Java cheat sheet

Java cheat sheet

Java on one page: types, strings, the collections you use, classes, control flow, and streams.

Variables and typesStringsCollectionsClassesControl flowStreams

← All cheat sheets

Java Cheat Sheet

Quick reference from FreeCodingCourses.com

Variables and types

int x = 5;
primitive int. Note the semicolon.
double d = 3.14; boolean ok = true;
other primitives.
String s = "hi";
String is a class, capital S.
var list = new ArrayList<String>();
var infers the type (Java 10+).
final int MAX = 100;
final means it cannot be reassigned.

Strings

s.length() s.charAt(0)
length and a character.
s.equals(other)
compare content. Never use == for strings.
s.toUpperCase() s.trim()
case change and trim.
s.split(",") String.join(",", list)
split and join.
String.format("Hi %s", name)
build a formatted string.

Collections

List<String> xs = new ArrayList<>();
a growable list.
xs.add("a"); xs.get(0); xs.size();
add, read, count.
Map<String,Integer> m = new HashMap<>();
key/value map.
m.put("a", 1); m.get("a");
set and read.
m.getOrDefault("b", 0)
read with a fallback.

Classes

public class User { private String name; }
a class with a field.
public User(String name) { this.name = name; }
a constructor.
public String getName() { return name; }
a getter method.
User u = new User("Ada");
create an instance.

Control flow

if (x > 0) { ... } else { ... }
parentheses and braces.
for (int i = 0; i < 5; i++) { ... }
classic counted loop.
for (String x : xs) { ... }
enhanced for: loop over items.
while (cond) { ... }
loop while true.
switch (x) { case 1 -> ...; default -> ...; }
arrow switch (Java 14+).

Streams

xs.stream().filter(x -> x > 0)
keep matching items.
.map(x -> x * 2)
transform each item.
.collect(Collectors.toList())
gather back into a list.
xs.forEach(System.out::println)
run something per item.

Unlock the full Java cheat sheet

You're seeing 2 of 6 sections. Drop your email to open the rest plus the gotchas, and save the whole thing as a printable PDF.

No spam, unsubscribe anytime. See our privacy policy.

Java questions, answered

Is Java the same as JavaScript?

No, and the names are a historical accident. Java is a compiled, statically typed language often used for large back-end systems and Android. JavaScript runs in the browser and on Node.js and is dynamically typed. Beyond a C-style syntax, they are unrelated.

Why do I compare strings with equals and not ==?

In Java, == checks whether two variables point to the exact same object in memory. Two different String objects with the same text are not the same object, so == can be false even when the text matches. .equals() compares the actual characters, which is what you almost always want.

What are streams for?

Streams let you process a collection in a readable pipeline: filter, map, then collect, instead of writing manual loops. They shine when you are transforming or filtering lists. Under the hood they still loop; they just read more clearly for data work.

Where to go next