Java

Data Types

int32-bit integer
long64-bit integer
double64-bit floating point
float32-bit floating point
booleantrue or false
charSingle character
byte8-bit integer
short16-bit integer
StringText (object type)
var x = valType inference (10+)

String Methods

.length()String length
.charAt(i)Character at index
.substring(start, end)Extract substring
.indexOf(str)Find substring index
.contains(str)Check if contains
.split(regex)Split into array
.trim()Remove whitespace
.replace(old, new)Replace substring
.toUpperCase()Uppercase conversion
.equals(str)Compare strings (not ==)
String.format("%s", val)Formatted string
String.valueOf(n)Convert to string

Collections

List<T> list = new ArrayList<>()Dynamic array
list.add(item)Add to list
list.get(i)Get by index
list.size()List length
list.remove(i)Remove by index
Map<K,V> map = new HashMap<>()Key-value map
map.put(k, v)Add to map
map.get(k)Get value by key
map.containsKey(k)Check key exists
Set<T> set = new HashSet<>()Unique collection
Collections.sort(list)Sort list
Collections.unmodifiableList()Immutable list

Control Flow

if (cond) { } else { }If-else statement
switch (val) { case: }Switch statement
for (int i=0; i<n; i++)For loop
for (T item : collection)Enhanced for-each
while (cond) { }While loop
do { } while (cond)Do-while loop
break / continueExit / skip iteration
cond ? a : bTernary operator

OOP

class MyClass { }Define a class
new MyClass()Create instance
extends BaseClassInheritance
implements InterfaceImplement interface
interface IName { }Define interface
abstract class A { }Abstract class
super()Call parent constructor
@OverrideOverride annotation
this.fieldCurrent instance field
static method()Class-level method

Access Modifiers

publicAccessible everywhere
privateClass only
protectedClass + subclasses
(default)Package-private
finalCannot override/reassign
staticBelongs to class

Exception Handling

try { } catch (E e) { }Try-catch block
finally { }Always executes
throw new Exception(msg)Throw exception
throws ExceptionDeclare throwable
try (Resource r = ...) { }Try-with-resources
catch (A | B e)Multi-catch
NullPointerExceptionNull access error
IllegalArgumentExceptionBad argument error

Streams & Lambda

list.stream()Create stream
.filter(x -> cond)Filter elements
.map(x -> transform)Transform elements
.collect(Collectors.toList())Collect to list
.forEach(x -> action)Iterate stream
.reduce(init, (a,b) -> a+b)Reduce to value
.sorted()Sort stream
.distinct()Remove duplicates
Optional<T>Nullable wrapper
Optional.ofNullable(val)Wrap nullable value

Common Operations

System.out.println()Print to console
Arrays.sort(arr)Sort array
Arrays.asList(arr)Array to list
Integer.parseInt(str)Parse string to int
Math.max(a, b)Maximum of two
Math.random()Random 0.0-1.0
allprintabledoc.com