How does Pattern Matching for instanceof work (Java 14)

Pattern Matching for instanceof simplifies the process of type checking and casting in Java. Introduced in Java 14 as a preview feature, it enhances code readability and reduces boilerplate code.
Java 14, Pattern Matching, instanceof, type checking, casting, code readability
public class PatternMatchingExample {
        public static void main(String[] args) {
            Object obj = "Hello, World!";
            if (obj instanceof String str) {
                System.out.println("String length: " + str.length());
            } else {
                System.out.println("Not a String");
            }
        }
    }
    

Java 14 Pattern Matching instanceof type checking casting code readability