This is an enhancement of Java language with pattern matching for the instanceof operator. Currently, this feature is in the second preview in JDK 15.
So currently when we need to cast an object, we check it using instanceof operation then only we cast the object to save our self from class cast exception.
In the above code snippet, we have to explicitly cast it to an object.
In this enhancement, we no more have to do it.
To run the below code you need to install JDK 14 or above and have to enable the preview feature of javac & java.
In order to enable that preview feature.
javac
--enable-preview
-release <version>
<Source file>
java
--enable-preview
<Compiled class>