Pattern Matching in Java 14 Adds Simplicity | TechWell

Pattern Matching in Java 14 Adds Simplicity

Pattern

As Java evolves and becomes more commonly used, it becomes essential to make Java easier to use for software development. Even after 25 years, Java has several features that are unwieldy and require a lot of code, which not only makes Java software difficult to develop but also too verbose for any practical implementation. In this article, we discuss one such feature, the instanceof operator, which is used to test if an object is a certain type. The instanceof matches a target object to a specified type for conditional processing of a subsequent block of code.

The conditional block typically includes the extraction of components from the target object. The extraction of components from the target object adds unnecessary code to convert an object to a specific type by casting and declaring a new variable. The additional code could also introduce errors.

Java 14 has introduced experimental support for pattern matching for the instanceof operator. A type test pattern is used for matching instead of just a type. The type test pattern consists of a predicate that specifies a type and a binding variable. The instanceof tests the predicate with the target object, and if the predicate applies to the target object successfully the binding variable/s is/are extracted from the object, which means that the binding variable is assigned a value.

As a before example consider:
if (obj instanceof String) {
     String s = (String) obj;
}

Next, consider the same example using the pattern matching feature:
if (obj instanceof String s) {
    //use variable s
    } else {// variable s is not available }

The instanceof matches the target object to the type test pattern (such as String s) for conditional processing of a subsequent block of code. In the example test pattern, if the predicate String applies to the target object successfully the target object is cast to String and assigned to the binding variable s. The binding variable in the type test pattern is assigned a value only if the predicate gets applied to the target object.

Essentially, if the target object is of a certain type, the unnecessary casting of the target object to the specified type and declaring a new variable is performed internally, rather than a user having to add code.

The binding variable is in the scope of only the true block of the if statement if the simplest semantics is used in the if condition. Unlike the scope of a local variable, the scope of the binding variable is determined by the semantics of the containing expressions and statements in the condition. The scope of the binding variable may not be obvious with a cursory review of the code.

The new feature we discussed is a preview language feature in Java 14. The feature will get added to the language based on user and developer feedback. As mentioned in an earlier article, preview language features are not recommended for production use. In Java 15 the new feature is still a preview language feature. As the pattern matching feature does remove a lot of unnecessary code, it would be a useful addition to the Java language.

Up Next

About the Author

TechWell Insights To Go

(* Required fields)

Get the latest stories delivered to your inbox every month.