Java 17: Pattern Matching For instanceof

We’ll talk about Java’s enhancement—the use of the instanceof keyword and the pattern-matching feature—in this post. Pattern matching in computer science is the process of determining if the elements of a given pattern are present in a given sequence of tokens. As with regular expressions, pattern matching is typically used to examine or test certain data to see if it matches or not a specific pattern. The Java functionality for pattern matching in the next JEP has been improved and is now shorter and more potent when used with the instanceof operator.

You might be wondering now what issue the aforementioned improvements will address. Therefore, the boilerplate code to type check and cast to a variable is eliminated by the pattern matching for instanceof operator. With the aid of a few examples, let’s learn about Pattern Matching for instanceof. Here, we’ll start with the standard method of utilizing the instanceof keyword.

1. Traditional instanceof keyword

All of us must have at some point created a program that would allow us to determine if you were working with an item of type A or B. In most cases, we could accomplish this by first casting, then using the instanceof operator, as shown below.

// Here is a simple Java Program to Illustrate instanceof Keyword

interface Animal {
}

class Cat implements Animal {
}

class Dog implements Animal {
}

public class App {

	public static void resolveTypeOfObject(Animal animal)
	{
		if (animal instanceof Cat) {
			// Redundant casting
			Cat cat = (Cat)animal;
			// other cat operations
		}
		else if (animal instanceof Dog) {
			// Redundant casting
			Dog dog = (Dog)animal;
			// other dog operations
		}
	}

	public static void main(String[] args)
	{
		Animal animal = new Dog();
		resolveTypeOfObject(animal);

		animal = new Cat();
		resolveTypeOfObject(animal);
	}
}



2 Pattern Matching

Due to the fact that we have tested for the instance in the if statement, the casting step appears to be superfluous. As demonstrated in the following section, the pattern-matching enhancement makes an effort to prevent this boilerplate code.

// Java Program to Illustrate Pattern matching for
// instanceof Keyword

public class DCW {

	public static void resolveTypeOfObject(Animal animal)
	{
		if (animal instanceof Cat cat) {
			cat.meow();
			// other cat operations
		}
		else if (animal instanceof Dog dog) {
			dog.woof();
			// other dog operations
		}
	}

	public static void main(String[] args)
	{
		Animal animal = new Dog();
		resolveTypeOfObject(animal);

		animal = new Cat();
		resolveTypeOfObject(animal);
	}
}



Let’s analyze what is going on in this situation. The animal type object will be compared to the type Pattern Cat object first, and if they are the same type, the object will be assigned to the cat variable.

We should exercise a little caution while determining the extent of these pattern variables. Keep in mind that the pattern variable is only assigned when the predicate test is successful. We are only allowed to access the variable inside of the enclosing if block.

As we can see, the size of the code has decreased and we are no longer required to typecast repeatedly. The readability of this version of the code is considerably improved, and it is a lot simpler to grasp.

One thought on “Java 17: Pattern Matching For instanceof

Comments are closed.