Java
数据类型
int—32-bit integer
long—64-bit integer
double—64-bit floating point
float—32-bit floating point
boolean—true or false
char—Single character
byte—8-bit integer
short—16-bit integer
String—Text (object type)
var x = val—Type inference (10+)
面向对象
.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
集合
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
异常
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 / continue—Exit / skip iteration
cond ? a : b—Ternary operator
输入输出
class MyClass { }—Define a class
new MyClass()—Create instance
extends BaseClass—Inheritance
implements Interface—Implement interface
interface IName { }—Define interface
abstract class A { }—Abstract class
super()—Call parent constructor
@Override—Override annotation
this.field—Current instance field
static method()—Class-level method
Access Modifiers
public—Accessible everywhere
private—Class only
protected—Class + subclasses
(default)—Package-private
final—Cannot override/reassign
static—Belongs to class
Exception Handling
try { } catch (E e) { }—Try-catch block
finally { }—Always executes
throw new Exception(msg)—Throw exception
throws Exception—Declare throwable
try (Resource r = ...) { }—Try-with-resources
catch (A | B e)—Multi-catch
NullPointerException—Null access error
IllegalArgumentException—Bad 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