With the release of Java 16, a major improvement was introduced with the introduction of Records (JEP 395), which allowed for a simpler and more concise way to declare classes that are primarily used to carry data. This improvement has now been further enhanced in Java 21, with the addition of Pattern Matching and Records (JEP 406). This new feature allows for the use of pattern matching to test whether a value is an instance of a Record class and extract its components in a more streamlined way. In this article, we will explore the changes brought about by Pattern Matching and Records in Java 21 and how it can benefit Java developers.
Records as Transparent Carriers for Data
Records, introduced in Java 16, are classes that are primarily used to store and carry data. They are transparent carriers, meaning that their main purpose is to hold data, and all other functionality such as constructors, methods, and equals/hashCode methods, are generated automatically by the compiler, based on the data fields defined in the record. This makes them ideal for use in scenarios where data needs to be serialized or sent over the network.
Consider the example of a Line class that defines two X and Y coordinates:
record Line(int x, int y) {}
To use this class, we can simply create an instance of the Line class and access its data fields using the built-in component accessor methods, x() and y():
Line line = new Line(0, 10);
int x = line.x();
int y = line.y();
System.out.println("X: " + x + ", Y: " + y); // Output: X: 0, Y: 10
Pattern Matching with Records
In Java 21, Pattern Matching has been added which makes it possible to test whether a value is an instance of a Record class and extract its components in a more streamlined way. This feature is especially useful when dealing with large codebases that use Records extensively.
Consider the following example where we want to test whether an object is an instance of the Line class and extract its components:
static void length(Object obj) {
if (obj instanceof Line l) {
int x = l.x();
int y = l.y();
System.out.println(y-x);
}
}
As you can see, we have used a type pattern to test whether the object is an instance of Point and if so, we have extracted its components by invoking the built-in component accessor methods. While this code works, it can be further simplified with the use of a record pattern in Java 21.
With record pattern, we can not only test whether a value is an instance of a Record class, but we can also extract its components in a single line of code. This is achieved by lifting the declaration of local variables for the extracted components into the pattern itself, and initializing those variables by invoking the accessor methods when the value is matched against the pattern.
Consider the following code that uses a record pattern:
static void length(Object obj) {
if (obj instanceof Line(int x, int y)) {
System.out.println(y-x);
}
}
This code is much more concise and readable. We have eliminated the need for creating a new object and invoking its component accessor methods to get the data. The record pattern directly extracts and initializes the components for us, making our code more streamlined.
Nested Record Patterns
One of the major challenges faced by developers is dealing with complex object graphs and extracting data from them. This is where the real power of pattern matching comes in, as it allows us to scale elegantly and match more complicated object graphs.
Consider the following classes: Employee, Department (enum), and Company (record). We can use a record pattern to extract the Department of an Employee from a Company object:
// As of Java 21
static void printEmployeeDepartment(Company c, String name) {
if (c instanceof Company(Department dept, List<Employee> employees)) {
for (Employee e : employees) {
if (e.getName().equals(name)) {
System.out.println(name + " is in " + dept + " department.");
return;
}
}
}
System.out.println(name + " not found.");
}
In this example, we are using nested patterns to extract the Department of an Employee from a Company object. We check if the given Company object has a Department and a list of Employees, and then loop through the list to find the Employee with the given name. If the Employee is found, we print their department. If not, we print a message saying that the Employee was not found.
Nested patterns can also be used in situations where we want to match and deconstruct multiple values at once. Consider the following example where we want to check if a given coordinate is located inside a rectangle:
//As of Java 21
record Point(double x, double y) {}
record Rectangle(Point upperLeft, Point lowerRight) {}
// check if given point is located inside the given rectangle
static boolean isPointInsideRectangle(Point p, Rectangle r) {
if (r instanceof Rectangle(Point(var x1, var y1), Point(var x2, var y2))) {
if (p.x() > x1 && p.y() > y1 && p.x() < x2 && p.y() < y2) {
return true;
}
}
return false;
}
In this example, we are using nested patterns to check whether a given Point object falls within the bounds of a given Rectangle object. The nested pattern allows us to access the x and y coordinates of the upper-left and lower-right points of the rectangle without having to write multiple lines of code.
In conclusion, with the addition of Pattern Matching and Records (JEP 406) in Java 21, there is a significant improvement in how we can handle and extract data from complex objects. This feature greatly simplifies code and makes it more readable and concise. It also helps in handling failure scenarios where the pattern match might fail. With these changes, Java 21 continues to make the code more streamlined and improves the development experience for Java developers.
Upgrade your Java 21 skills with MyExamCloud's Java SE 21 Developer Professional Practice Tests. Develop and test your knowledge to be a Java 21 expert.
Top comments (0)