Sunday, 23 May 2021

Java14: Pattern matching for instanceof

Java14 enhances the instanceof operator by adding pattern matching functionality. This is added as preview feature in Java14.

 

For example,

if (obj instanceof String) {
	String s = (String) obj;
	System.out.println("Length of the string s is " + s.length());
}

 

In the above snippet we are performing three operations.

a.   Checking whether the variable ‘obj’ is instance of String or not.

b.   Explicitly type casting obj to String

c.    Defining new variable s

 

Above statement can be written using pattern matching like below.

if (obj instanceof String s) {
	System.out.println("Length of the string s is " + s.length());
}

 

In Java14, the instanceof operator is extended to take a type test pattern instead of just a type. In the code above, the phrase String s is the type test pattern. If ‘obj’ is an instance of String, then it is cast to String and assigned to the binding variable s.

 

Binding variable ‘s’ scope is limited to the true block and not available outside of it.

if (obj instanceof String s) {
	// You can use s here
} else {
	// You can't use s here
}

 

InstanceOfPatternDemo.java

public class InstanceOfPatternDemo {
	public static void main(String args[]) {
		
		Object obj = "Hello World";
		
		if (obj instanceof String s) {
		    System.out.println("Length of the string s is " + s.length());
		} else {
		   System.out.println("obj is not instance of String");
		}
	}
}

 

Output

Length of the string s is 11

 

Scope of binding variable is derived by the semantics

The scope of a binding variable, is determined by the semantics of the containing expressions and statements. For example, in this code:

if (! (obj instanceof String s)) {
	// You can't use s here
} 
else {
	// You can use s here
}

 

InstanceOfPatternDemo2.java

public class InstanceOfPatternDemo2 {
	public static void main(String args[]) {
		
		Object obj = "Hello World";
		
		if (! (obj instanceof String s)) {
			System.out.println("obj is not instance of String");
		} else {
			System.out.println("Length of the string s is " + s.length());
		}
	}
}

 

Output

Length of the string s is 11

 

The scope of binding variable grows as the conditional expression grows.

 

Example

 

if ((obj instanceof String s) && s.length() > 10) {
	System.out.println("obj is instance of String and length is > 10");
} else if ((obj instanceof String s)){
	System.out.println("obj is instance of String and length is <= 10");
}else {
	System.out.println("Object is not an instance of String");
}

 

InstanceOfPatternDemo3.java

public class InstanceOfPatternDemo3 {
	public static void main(String args[]) {
		
		Object obj = "Hello World";
		
		if ((obj instanceof String s) && s.length() > 10) {
			System.out.println("obj is instance of String and length is > 10");
		} else if ((obj instanceof String s)){
			System.out.println("obj is instance of String and length is <= 10");
		}else {
			System.out.println("Object is not an instance of String");
		}
	}
}

 

Output

obj is instance of String and length is > 10

 

How instanceof works when the obj is null?

There is no change in the behavior of instanceof operation when the obj is null. The pattern will only match, and s will only be assigned, if obj is not null.

 

InstanceOfPatternDemo4.java

public class InstanceOfPatternDemo4 {
	public static void main(String args[]) {
		
		Object obj = null;
		
		if ((obj instanceof String s) && s.length() > 10) {
			System.out.println("obj is instance of String and length is > 10");
		} else if ((obj instanceof String s)){
			System.out.println("obj is instance of String and length is <= 10");
		}else {
			System.out.println("Object is not an instance of String");
		}
	}
}

 

Output

Object is not an instance of String

 

 

Reference

https://openjdk.java.net/jeps/305

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment