The Record Patterns feature was initially proposed as a preview feature in JDK 19 by JEP 405. It was then previewed again in JDK 20 by JEP 432. This feature evolved alongside Pattern Matching for switch (JEP 441) and is now being finalized based on ongoing experience and feedback.
What is Record Patterns?
The Record Patterns feature to enhance the Java language's ability to deconstruct record values (Records JEP 395). These patterns allow nested and powerful data navigation and processing.
The motivation behind Record Patterns is Pattern Matching for instanceof,
The Pattern Matching for instanceof (JEP 305) was released as a preview feature in Java JDK 14 and was re-proposed as JEP 375 and delivered in JDK 15 as a second round of preview and was finalized as a feature in JDK 16 (JEP 394).
Examples with Pattern Matching for instanceof
Before Java 16:
public static void main(String[] args) {
Object object = "Code2care";
if (object instanceof String) {
String str = (String) object;
System.out.println("String Length: " + str.length());
}
}
Java 16: Pattern Matching (As preview in Java 14)
public static void main(String[] args) {
Object object = "Code2care!";
if (object instanceof String str) {
System.out.println("String Length: " + str.length());
}
}
public static void main(String[] args) {
Object object = "Code2care";
if (object instanceof String) {
String str = (String) object;
System.out.println("String Length: " + str.length());
}
}
public static void main(String[] args) {
Object object = "Code2care!";
if (object instanceof String str) {
System.out.println("String Length: " + str.length());
}
}
As you can see in Java 16 the pattern matching makes the code concise, you can now directly use 'str' as a String here, there is no need for casting.
Examples of Record Patterns
Example: Java 16
public class RecordPatternsExample {
record Sum(int no1, int no2) {}
public static void main(String[] args) {
Sum sum = new Sum(10, 20);
printSum(sum);
}
static void printSum(Object obj) {
if (obj instanceof Sum p) {
int x = p.no1();
int y = p.no2();
System.out.println(x+y);
}
}
}
Java 21: Record Patterns
public class RecordPatternsExample {
record Sum(int no1, int no2) {}
public static void main(String[] args) {
Sum sum = new Sum(10, 20);
printSum(sum);
}
static void printSum(Object obj) {
if (obj instanceof Sum(int no1, int no2)) {
System.out.println(no1+no2);
}
}
public class RecordPatternsExample {
record Sum(int no1, int no2) {}
public static void main(String[] args) {
Sum sum = new Sum(10, 20);
printSum(sum);
}
static void printSum(Object obj) {
if (obj instanceof Sum p) {
int x = p.no1();
int y = p.no2();
System.out.println(x+y);
}
}
}
public class RecordPatternsExample {
record Sum(int no1, int no2) {}
public static void main(String[] args) {
Sum sum = new Sum(10, 20);
printSum(sum);
}
static void printSum(Object obj) {
if (obj instanceof Sum(int no1, int no2)) {
System.out.println(no1+no2);
}
}
The code with Record Patterns is more readable as it allows both checking the type and extracting components of record values in a single step. This streamlines code and eliminates the need for explicit type casting, resulting in cleaner and more maintainable code.
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- Installing Java JDK 21 Final Release Candidate
- Steps to Install Java 21 (LTS JDK) on Windows 11
- How to install and Use Java JDK 21 Initial Release Candidate
- How to Enable Java JDK 21 Preview Features on IntelliJ
- Setting Up VS Code with Java JDK 21
- Java JDK 21 LTS Version Release Date (General Availability)
- [JEP 430] Java JDK 21 New Feature - String Templates (Preview)
- How to use Java JDK 21 with IntelliJ
- Java JDK 21: JEP 439 - An Improved Generational Z Garbage Collector (ZGC)
- [JEP 431] Java JDK 21 New Feature - Sequenced Collections
- Java JDK 21 - The Latest LTS Version
- Java JDK 21 - JEP 440 - Record Patterns
- Fix - Unsupported major.minor version 65.0 (Java JDK 21)
- Fix java.net.ProtocolException: Invalid HTTP method - Java
- Format Code in Visual Studio - VS Code [Mac/Windows/Linux] - HowTos
- JUnit: Failing Test on NullPointerException Example - Java
- Teams - You're offline. Messages you send while offline will be sent when you're back online - Teams
- Add Animated Scrolling to Html Page Title Script - JavaScript
- How to Skip or Ignore JUnit test cases in Java - Java
- Step-by-Step: Setting up Docker + Ubuntu Linux + Git + GitHub Tutorial - Git
- [Android] This view is not constrained vertically: at runtime it will jump to the top unless you add a vertical constraint - Android