Why Is Everyone Excited About Pattern Matching In Java 14?

March 24, 2020
pattern matching for java 14



Java 14, or Java Development Kit (JDK) 14 is now being released for general use. Everyone in the Java Developer Community seems very excited by its major updates, including the JDK flight recorder, event streaming, and switch expressions. The one we focus on in this article is pattern matching.  WIth Xperti, you can get a clear picture and can understand Why Is Everyone Excited About Pattern Matching In Java 14 and what is the significance of InstanceOf

Also Read: PASSIVE RECRUITMENT: 7 Best Practices for IT Recruiters

Pattern Matching For Java Developers

Pattern matching is the checking and pinpointing of a specific sequence or pattern of data from raw data. This is not to be confused with pattern recognition, as the match has to be the same in the case of pattern matching, whereas exactitude is not needed in the case of pattern recognition. Pattern matching is one of the most fundamental and important paradigms in several programming languages. There are many applications for pattern matching in computer science. High-level language compilers (like Java) make use of pattern matching to parse code to determine if it is syntactically correct.

Use of“InstanceOf” Operator

Pattern matching—as included in Java 14–is to be used with the “instanceof” operator. Pattern matching allows the detection of common logic in the code. As a result, the code can be made shorter, less recurrent and simple to read and write. At some point, every Java Developer has used the ‘instanceof’ operator in his/her code to compare a reference variable. If the result turns out to be true, the process follows by explicitly casting it to the type you compared it with, to access its members. At this point, we can extract our variables before we apply any further processing specifics to that type.

if (vehicle instanceof Land) {
Land car = (Land) vehicle;
car.sports();
// other car operations
} else if (vehicle instanceof Water) {
Water ship = (Water) vehicle;
ship.cruise();
 // other ship operations
}

In the code above, for every conditional block, we are testing the vehicle parameter to decide its type. But first, we need to convert it via a cast, then we can declare a local variable. We can perform operations specific to this vehicle only after these steps are complete.

Does this Work?

Although this method works well, and developers have been using it for a long time, the top software developers in the Java Developer Community proactively drew attention to the fact that this is a very repetitive process. Readability is also very poor which makes it tiresome to work with, especially when the type needs to be tested. The problem exacerbates if we add a new vehicle.

A new pattern variable with the ‘instanceof’ operator is introduced in Java 14 via JEP 305. If the instanceof condition turns out to be true, the pattern variable is automatically bound to the variable that was compared, thus eliminating the need for explicit casting to use its members. As a result, Java Coders can conditionally extract components from objects. It takes less time and is comparatively safer.

Here is the same code with the improved instanceof operator

if (vehicle instanceof Land car) {
car.sports();
} else if (vehicle instanceof Water ship) {
ship.steam();
}

Now here is the first, “if block”, we are matching vehicle against the type pattern “land car”. First, we test the vehicle variable to see if it’s an instance of land. If it is true, it’ll be cast to our land type, and finally, we will assign the result to the car.

One thing to understand here is that the variable names “car and ship” are not existing variables, they are declared pattern variables. If we attempt to use either variable in another location, a compiler error will be displayed, because these variables are also only in scope and they will be assigned when the respective pattern match expressions return true.

Scope of Java 14

This version of the code is much more concise and easier to understand. It has been simplified and is much more readable now that the explicit casting has been removed. However, the scope of the pattern variable is currently limited. We cannot access it in the else block.

If we do this, an error will be generated by the compiler as Pattern Matching has been released as a preview language. What this means for Java Developers, in general, is that even though this feature is ready to be used by developers, it can be changed in future Java releases. Any changes that are made will reflect the feature feedback provided by software developers.

Conclusion

Pattern matching is just one of many new features in the new Java release that saves time and simplifies code. They’ve been received with both relief and excitement by a community of top technology experts, eager to do more in development. If there had ever been speculation that Java was a dying language, those rumors have been put to rest.



author

nayyara-rahman

Nayyara Rahman is a management and technology professional with a focus on digital services. Her work in integrating marketing and technology is aimed at making organizations more efficient, accountable and transparent. She is also an award-winning author and researcher whose contribution has been acknowledged on several prestigious international forums.


Candidate signup

Create a free profile and find your next great opportunity.

JOIN NOW

Employer signup

Sign up and find a perfect match for your team.

HIRE NOW

How it works

Xperti vets skilled professionals with its unique talent-matching process.

LET’S EXPLORE

Join our community

Connect and engage with technology enthusiasts.

CONNECT WITH US