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.