GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Record patterns (destructuring) | java.evolved

Home / Language / Record patterns (destructuring)
Language Intermediate

Record patterns (destructuring)

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

Destructure records directly in patterns — extract fields in one step.

Records Pattern Matching
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);
}
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
🎯

Direct extraction

Access record components without calling accessors manually.

🪆

Nestable

Patterns can nest — match inner records in a single expression.

📏

Compact code

Five 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
Record Patterns (JEP 440) ↗
Proof
View proof source ↗
Related patterns
Language Beginner

Diamond with anonymous classes

Java 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) {..}
    };
Hover to see modern ➜
JDK 9+ →
Language Beginner

Records for data classes

Java 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) {}
Hover to see modern ➜
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);
});
Hover to see modern ➜
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.