Java 21 Record Patterns
Java 21 brings powerful improvements with Record Patterns that enhance pattern matching capabilities. These features allow you to destructure objects directly in conditionals, making code cleaner and more expressive.
๐ What Are Record Patterns?
Record Patterns allow you to deconstruct a record into its components inside a pattern matching construct. This feature works in instanceof
, switch
expressions, and more.
Example:
record Point(int x, int y) {}
void print(Point p) {
if (p instanceof Point(int x, int y)) {
System.out.println("x = " + x + ", y = " + y);
}
}
Before Java 21, you had to call p.x()
and p.y()
manually. Now you can extract values inline!
๐ฏ Why Use Record Patterns?
- ✅ More concise and readable code
- ✅ Reduces boilerplate and null checks
- ✅ Enables more declarative and functional-style programming
- ✅ Integrates smoothly with switch expressions
๐ญ Record Patterns in instanceof
Using pattern matching with instanceof
and records becomes extremely powerful.
if (obj instanceof Person(String name, int age)) {
System.out.println("Name: " + name + ", Age: " + age);
}
No need for type casts or manual getters!
๐ Record Patterns in switch
You can use record patterns directly in switch
statements and expressions.
record Shape() {}
record Circle(double radius) extends Shape {}
record Rectangle(double width, double height) extends Shape {}
void describe(Shape shape) {
switch (shape) {
case Circle(double r) -> System.out.println("Circle with radius " + r);
case Rectangle(double w, double h) -> System.out.println("Rectangle " + w + " x " + h);
default -> System.out.println("Unknown shape");
}
}
This makes switch
more powerful than ever in Java.
๐ฆ Nested Record Patterns
You can destructure nested records using patterns inside patterns!
record Address(String city, String zip) {}
record User(String name, Address address) {}
if (u instanceof User(String name, Address(String city, String zip))) {
System.out.println(name + " lives in " + city + " " + zip);
}
This feature allows fine-grained matching in deeply nested data structures.
⚠️ Limitations and Notes
- Only works with
record
types - Must enable preview features in Java 21
- May not yet support full guard conditions in all IDEs
๐งช Enabling Record Patterns
To use Record Patterns, make sure you're on Java 21 and compile with:
javac --enable-preview --release 21 YourFile.java
java --enable-preview YourFile
๐ก Best Practices
- ✅ Use record patterns to simplify code with complex conditions
- ✅ Favor switch with patterns for better control flow
- ✅ Document the match structure if it gets too nested
- ✅ Keep logic inside patterns clean and side-effect free
๐ Final Thoughts
Java 21’s Record Patterns represent a major step forward in making Java more expressive and modern. It encourages cleaner design, better control flow, and concise data handling. This feature, combined with switch enhancements, is a must-learn for any modern Java developer.
๐ Related Posts
๐ Java 21 Features
Explore all the new features introduced in Java 21, including record patterns, sequenced collections, and more.
๐ก Exception Handling Best Practices
Improve your Java code quality with effective exception strategies and clean error modeling.
๐ง Java 21 Interview Qs
Test your knowledge of Java 21’s features including records, pattern matching, and virtual threads.
๐ Mastering Java Streams
A practical guide to Java Streams API — map/filter/reduce, collectors, and parallel streams.