Records & Pattern Matching in Java
From the Java cheat sheet · Modern Java (25) · verified Jul 2026
Records & Pattern Matching
Deconstruct records and match by shape.
java
// Quick Reference
record Point(int x, int y) {}
String s = switch (obj) {
case Point(int x, int y) -> x + "," + y; // record pattern
default -> "other";
};💡 Record patterns destructure a record into its components inline - no accessors.
⚡ Patterns nest: match a Line and pull out both Points and their coordinates at once.
📌 var inside a pattern infers each component type, keeping matches concise.
🟢 Sealed types + record patterns give the compiler exhaustiveness checks in switch.
recordspattern-matching