Enhanced instanceof pattern matching, introduced in Java 16 (as a preview feature) and finalized in Java 17, allows you to combine type checking with type casting, reducing boilerplate code and making it more concise and readable.
Here’s how you can use enhanced instanceof pattern matching:
- Basic Usage:
Instead of separately checking if an object is an instance of a class and then casting it, you can do both in one step using the pattern matching feature. The syntax is:if (obj instanceof Type variableName) { // variableName is automatically cast to Type }Example:
Object obj = "Hello, Java!"; if (obj instanceof String str) { // This checks and casts obj to String System.out.println("String length: " + str.length()); } else { System.out.println("Not a string."); }This eliminates the need for explicit type casting.
-
Combine with Logical Operators:
You can combine the pattern matching with additional conditions using logical operators like&&or||.Example:
Object obj = "Patterns in Java"; if (obj instanceof String str && str.length() > 10) { System.out.println("String is longer than 10 characters: " + str); } else { System.out.println("String is too short or not a string at all."); }In this case, the
strvariable is only in scope if both conditions are true. -
Scope of the Pattern Variable:
- The pattern variable (e.g.,
strin the examples above) is only accessible within the block where the pattern matching is true. - Outside of the
ifblock, the variable doesn’t exist.
- The pattern variable (e.g.,
- Negating with
!instanceof:
Pattern matching itself cannot be negated directly (no “not instanceof”), but you can invert the condition like this:if (!(obj instanceof String)) { System.out.println("Not a string."); } - Using Pattern Matching in
switch:
Starting from Java 17 (as a preview) and improved in later versions, you can use pattern matching inswitchstatements for more powerful expressions. For example:Object obj = "Java 17"; switch (obj) { case String str && str.length() > 5 -> System.out.println("Long string: " + str); case String str -> System.out.println("Short string: " + str); default -> System.out.println("Not a string."); }This allows a combination of pattern matching and conditionals directly within
switch.
Benefits of Enhanced instanceof Pattern Matching
- Reduction of Boilerplate Code: By avoiding explicit casting and declaring new variables.
- Improved Readability: Simplifies conditional checks by combining the instance check and cast in one step.
- Type Safety: Provides better compile-time safety for the variables you use after a cast.
Recap of the Code Features
From the files you’ve referenced:
PatternMatchingExample.javademonstrates simple pattern matching withinstanceof, where the type check and assignment are done in one step.PatternMatchingExampleCombine.javashows combining pattern matching with additional conditions (e.g.,&&).
Both examples illustrate the practical and concise approach to type checking and casting introduced via enhanced instanceof pattern matching.
