GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev — Record patterns (destructuring) | java.evolved
Language
Intermediate
Record patterns (destructuring)Destructure records directly in patterns — extract fields in one step. Code Comparison
✕ Java 8
if (obj instanceof Point) {
Point p = (Point) obj;
int x = p.getX();
int y = p.getY();
System.out.println(x + y);
}
✓ Java 21+
if (obj instanceof Point(int x, int y)) {
IO.println(x + y);
}
See a problem with this code? Let us know.
Why the modern way wins
🎯
Direct extractionAccess record components without calling accessors manually. 🪆
NestablePatterns can nest — match inner records in a single expression. 📏
Compact codeFive lines become two — less ceremony, same clarity. Old Approach
Manual Access
Modern Approach
Destructuring
Since JDK
21
Difficulty
Intermediate
JDK Support
Record patterns (destructuring)
Available
Widely available since JDK 21 LTS (Sept 2023) How it works
Record patterns let you decompose a record's components directly in instanceof and switch. Nested patterns are supported too, enabling deep matching without intermediate variables. Related Documentation
Proof
Related patterns
Language
Beginner
Diamond with anonymous classesJava 7/8
Map<String, List<String>> map =
new HashMap<String, List<String>>();
// anonymous class: no diamond
Predicate<String> p =
new Predicate<String>() {
public boolean test(String s) {..}
};
Java 9+
Map<String, List<String>> map =
new HashMap<>();
// Java 9: diamond with anonymous classes
Predicate<String> p =
new Predicate<>() {
public boolean test(String s) {..}
};
JDK 9+
→
Language
Beginner
Records for data classesJava 8
public class Point {
private final int x, y;
public Point(int x, int y) { ... }
public int getX() { return x; }
public int getY() { return y; }
// equals, hashCode, toString
}
Java 16+
public record Point(int x, int y) {}
JDK 16+
→
Language
Beginner
Unnamed variables with _Java 8
try {
parse(input);
} catch (Exception ignored) {
log("parse failed");
}
map.forEach((key, value) -> {
process(value); // key unused
});
Java 22+
try {
parse(input);
} catch (Exception _) {
log("parse failed");
}
map.forEach((_, value) -> {
process(value);
});
JDK 22+
→
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |