Java Cheat Sheet
A quick reference of the most used Java syntax. Use it to revise before an exam or an interview. Every example is tested on Java 25.
Program
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Output
Hello, Java!
Variables and types
| Type | Example |
|---|---|
| int, long | int n = 5; long big = 5_000_000_000L; |
| double, float | double d = 3.14; float f = 3.14f; |
| char, boolean | char c = 'A'; boolean ok = true; |
| String | String s = "text"; |
| var | var list = new ArrayList<String>(); |
| Constant | final int MAX = 10; |
| Cast | int x = (int) 3.9; |
Operators
| Group | Operators |
|---|---|
| Arithmetic | + - * / % ++ -- |
| Comparison | == != > < >= <= |
| Logical | && || ! |
| Assignment | = += -= *= /= %= |
| Ternary | cond ? a : b |
Control flow
public class Flow {
public static void main(String[] args) {
int n = 7;
if (n > 5) System.out.println("big"); else System.out.println("small");
String name = switch (n % 3) {
case 0 -> "zero";
case 1 -> "one";
default -> "two";
};
System.out.println(name);
for (int i = 0; i < 3; i++) System.out.print(i + " ");
for (String s : new String[]{"a", "b"}) System.out.print(s + " ");
int k = 0;
while (k < 2) { System.out.print("w" + k + " "); k++; }
System.out.println();
}
}
Output
big
one
0 1 2 a b w0 w1
Strings
| Code | Result |
|---|---|
s.length() | Number of characters |
s.charAt(0) | First character |
s.substring(1, 3) | Part of the text |
s.indexOf("x") | Position, or -1 |
s.equals(t) | Compare the text |
s.split(",") | Array of parts |
String.format("%.2f", d) | Formatted text |
Arrays and collections
import java.util.*;
public class Coll {
public static void main(String[] args) {
int[] arr = {3, 1, 2};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
List<String> list = new ArrayList<>(List.of("b", "a"));
Collections.sort(list);
System.out.println(list);
Map<String, Integer> map = new HashMap<>();
map.put("x", 1);
map.merge("x", 5, Integer::sum);
System.out.println(map);
Set<Integer> set = new TreeSet<>(List.of(3, 1, 3));
System.out.println(set);
}
}
Output
[1, 2, 3]
[a, b]
{x=6}
[1, 3]
Classes
interface Speaker { String speak(); }
abstract class Animal implements Speaker {
protected final String name;
Animal(String name) { this.name = name; }
}
class Dog extends Animal {
Dog(String name) { super(name); }
@Override public String speak() { return name + " says Woof"; }
}
record Point(int x, int y) { }
enum Level { LOW, HIGH }
public class Cls {
public static void main(String[] args) {
System.out.println(new Dog("Tommy").speak());
System.out.println(new Point(1, 2));
System.out.println(Level.HIGH);
}
}
Output
Tommy says Woof
Point[x=1, y=2]
HIGH
Exceptions
public class Exc {
public static void main(String[] args) {
try {
Integer.parseInt("x");
} catch (NumberFormatException e) {
System.out.println("bad number");
} finally {
System.out.println("always");
}
}
}
Output
bad number
always
Lambdas and streams
import java.util.List;
public class Lam {
public static void main(String[] args) {
List<Integer> n = List.of(1, 2, 3, 4, 5, 6);
System.out.println(n.stream().filter(x -> x % 2 == 0).map(x -> x * x).toList());
System.out.println(n.stream().mapToInt(Integer::intValue).sum());
}
}
Output
[4, 16, 36]
21
Commands
javac Main.java # compile
java Main # run the compiled class
java Main.java # compile and run a single file
java -version # Java version
Where to go next
- Practice with the Java exercises and the interview questions of this website.
- Learn a build tool (Maven or Gradle) and a framework like Spring Boot.
- Build small projects: a to do list, a bank account, a library system.
Note:
Revise one section every day. Writing the code by hand is the best way to remember it.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |