Java Record Patterns
In this page:
record RecordName(Type a, Type b) { }
if (object instanceof RecordName(Type a, Type b)) {
// a and b are extracted
}
What are Record Patterns?
Record patterns, finalized in Java 21, let a record be deconstructed directly inside an instanceof or switch, extracting its components into named variables in the same expression instead of calling each accessor method separately.
Example: What are Record Patterns?
public class Main {
record Point(int x, int y) {}
public static void main(String[] args) {
Object obj = new Point(3, 4);
if (obj instanceof Point(int x, int y)) { // deconstructed in the same expression
System.out.println(x + "," + y);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Basic Record Deconstruction
Deconstructed components like x and y from a Point are immediately usable as normal local variables within the same condition, including in further boolean expressions combined with the pattern check.
Example: Basic Record Deconstruction
public class Main {
record Point(int x, int y) {}
public static void main(String[] args) {
Object obj = new Point(5, 10);
if (obj instanceof Point(int x, int y) && x > 0) { // usable in further conditions
System.out.println("Positive x: " + x);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Nested Record Patterns
Record patterns can nest, deconstructing a record that itself contains other records in a single pattern, reaching directly into deeply nested structures without a chain of manual accessor calls.
Example: Nested Record Patterns
public class Main {
record Point(int x, int y) {}
record Line(Point start, Point end) {}
public static void main(String[] args) {
Object obj = new Line(new Point(0, 0), new Point(5, 5));
if (obj instanceof Line(Point(int x1, int y1), Point(int x2, int y2))) { // nested deconstruction
System.out.println(x1 + "," + y1 + " -> " + x2 + "," + y2);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Record Patterns with var
The var keyword can be used in place of an explicit type for any component in a record pattern, letting the compiler infer the type from the record's own definition and keeping the pattern shorter.
Example: Record Patterns with var
public class Main {
record Point(int x, int y) {}
public static void main(String[] args) {
Object obj = new Point(7, 8);
if (obj instanceof Point(var x, var y)) { // type inferred from the record definition
System.out.println(x + "," + y);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Record Patterns in Switch
Record patterns work especially well combined with switch expressions over a sealed hierarchy, deconstructing each possible record type in its own case branch, and a when clause can add extra guard conditions to a specific case.
Example: Record Patterns in Switch
// Define the class Main
public class Main {
sealed interface Shape permits Circle, Square {}
// Define the record Circle (an immutable data carrier)
record Circle(double radius) implements Shape {}
// Define the record Square (an immutable data carrier)
record Square(double side) implements Shape {}
static String describe(Shape s) {
// Send switch (s) { back to the caller
return switch (s) {
case Circle(double r) when r > 10 -> "Big circle";
case Circle(double r) -> "Circle r=" + r;
case Square(double side) -> "Square side=" + side;
};
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(describe(new Circle(5)));
}
}
Login to try C/C++/Java/PHP code in the editor
- Writing a pattern with the wrong number of components, like
Point(int x)for a two-component record, which is a compile error. - Using a wrong type for a component, such as
Point(String x, int y)forrecord Point(int x, int y), which does not compile. - Trying to deconstruct a normal class with a record pattern, which only works for records.
- Java 8 features introduced lambdas and streams, and later releases added
var, records, and sealed classes. - Pattern matching, text blocks, switch expressions, and record patterns make code shorter and clearer.
- The module system, virtual threads, structured concurrency, and sequenced collections address larger applications and concurrency.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: