Java Cheat Sheet & Quick Reference
Java Cheat Sheet & Quick Reference
Java
This cheat sheet is a crash course for Java beginners and help review the basic syntax
of the Java language.
# Getting Started
Hello.java
$ javac Hello.java
$ java Hello
Hello, world!
Variables
https://quickref.me/java 1/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
int num = 5;
float floatNum = 5.99f;
char letter = 'D';
boolean bool = true;
String site = "quickref.me";
Strings
See: Strings
Loops
https://quickref.me/java 2/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
System.out.print(c + "-");
}
// Outputs: Q-u-i-c-k-R-e-f-
See: Loops
Arrays
See: Arrays
Swap
int a = 1;
int b = 2;
System.out.println(a + " " + b); // 1 2
int temp = a;
a = b;
b = temp;
System.out.println(a + " " + b); // 2 1
Type Casting
// Widening
// byte<short<int<long<float<double
int i = 10;
long l = i; // 10
// Narrowing
double d = 10.02;
https://quickref.me/java 3/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
long l = (long)d; // 10
String.valueOf(10); // "10"
Integer.parseInt("10"); // 10
Double.parseDouble("10"); // 10.0
Conditionals
int j = 10;
if (j == 10) {
System.out.println("I get printed");
} else if (j > 10) {
System.out.println("I don't");
} else {
System.out.println("I also don't");
}
See: Conditionals
User Input
# Java Strings
Basic
https://quickref.me/java 4/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
Concatenation
StringBuilder
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| | | | | | | | | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9
sb.append("QuickRef");
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| Q | u | i | c | k | R | e | f | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9
sb.delete(5, 9);
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| Q | u | i | c | k | | | | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9
https://quickref.me/java 5/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| M | y | | Q | u | i | c | k | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9
sb.append("!");
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| M | y | | Q | u | i | c | k | ! |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
Comparison
s1 == s2 // false
s1.equals(s2) // true
"AB".equalsIgnoreCase("ab") // true
Manipulation
str.toUpperCase(); // ABCD
str.toLowerCase(); // abcd
str.concat("#"); // Abcd#
str.replace("b", "-"); // A-cd
Information
https://quickref.me/java 6/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
str.charAt(2); // c
str.indexOf("a") // 0
str.indexOf("z") // -1
str.length(); // 4
str.toString(); // abcd
str.substring(2); // cd
str.substring(2,3); // c
str.contains("c"); // true
str.endsWith("d"); // true
str.startsWith("a"); // true
str.isEmpty(); // false
Immutable
// Outputs: hello
System.out.println(str);
// Outputs: helloworld
System.out.println(concat);
# Java Arrays
Declare
int[] a1;
int[] a2 = {1, 2, 3};
https://quickref.me/java 7/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
Modify
a[0] = 9;
System.out.println(a[0]); // 9
System.out.println(a.length); // 3
Loop (Read)
Multidimensional Arrays
https://quickref.me/java 8/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
int x = matrix[1][0]; // 4
// [[1, 2, 3], [4, 5]]
Arrays.deepToString(matrix)
Sort
// [a, b, c]
Arrays.toString(chars);
# Java Conditionals
Operators
https://quickref.me/java 9/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
+ - * /
% = ++ --
If else
int k = 15;
if (k > 20) {
System.out.println(1);
} else if (k > 10) {
System.out.println(2);
} else {
System.out.println(3);
}
Switch
int month = 3;
String str;
switch (month) {
case 1:
str = "January";
break;
case 2:
str = "February";
break;
case 3:
str = "March";
break;
default:
str = "Some other month";
break;
}
https://quickref.me/java 10/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
Ternary operator
int a = 10;
int b = 20;
int max = (a > b) ? a : b;
// Outputs: 20
System.out.println(max);
# Java Loops
For Loop
https://quickref.me/java 11/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
While Loop
int count = 0;
Do While Loop
int count = 0;
do {
System.out.print(count);
count++;
} while (count < 5);
// Outputs: 01234
Continue Statement
Break Statement
https://quickref.me/java 12/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
break;
}
}
// Outputs: 0123
Thread
Collection Interface Ordered Sorted Duplicate
safe
ArrayList List Y N N Y
Vector List Y N Y Y
List,
LinkedList Y N N Y
Deque
CopyOnWriteArrayList List Y N Y Y
HashSet Set N N N N
LinkedHashSet Set Y N N N
TreeSet Set Y Y N N
CopyOnWriteArraySet Set Y N Y N
ConcurrentSkipListSet Set Y Y Y N
https://quickref.me/java 13/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
Thread
Collection Interface Ordered Sorted Duplicate
safe
ArrayDeque Deque Y N N Y
PriorityQueue Queue Y N N Y
ConcurrentLinkedQueue Queue Y N Y Y
ConcurrentLinkedDeque Deque Y N Y Y
ArrayBlockingQueue Queue Y N Y Y
Li k dBl ki D D Y N Y Y
ArrayList
// Adding
nums.add(2);
nums.add(5);
nums.add(8);
// Retrieving
System.out.println(nums.get(0));
https://quickref.me/java 14/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
System.out.println(nums.get(i));
}
nums.remove(nums.size() - 1);
nums.remove(0); // VERY slow
HashMap
// Retrieving
System.out.println(m.get(6));
// Lambda forEach
m.forEach((key, value) -> {
String msg = key + ": " + value;
System.out.println(msg);
});
HashSet
set.add("dog");
set.add("cat");
set.add("mouse");
set.add("snake");
https://quickref.me/java 15/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
set.add("bear");
if (set.contains("cat")) {
System.out.println("Contains cat");
}
set.remove("cat");
for (String element : set) {
System.out.println(element);
}
ArrayDeque
// Using add()
a.add("Dog");
// Using addFirst()
a.addFirst("Cat");
// Using addLast()
a.addLast("Horse");
// Access element
System.out.println(a.peek());
// Remove element
System.out.println(a.pop());
https://quickref.me/java 16/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
# Misc
Access Modifiers
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
Regular expressions
// Splitting a String
text.split("\\|");
text.split(Pattern.quote("|"));
Comment
/*
And I am a
multi-line comment!
*/
/**
* This
* is
https://quickref.me/java 17/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
* documentation
* comment
*/
Keywords
super while
Math methods
Math.sqrt(a) Square-root of a
Math.pow(a,b) Power of b
https://quickref.me/java 18/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
Try/Catch/Finally
try {
// something
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("always printed");
}
Related Cheatsheet
C# Cheatsheet
Quick Reference
Recent Cheatsheet
https://quickref.me/java 19/20
4/30/24, 8:31 PM Java Cheat Sheet & Quick Reference
https://quickref.me/java 20/20