I came across this amazing topic on https://www.baeldung.com/java-pattern-matching-instanceof. But when I try to run the following code, it throws compile time error:
if(obj instanceof String s) {
System.out.println(s);
}
The error says:
Patterns in 'instanceof' are not supported at language level '14'
Error:(36, 34) java: pattern matching in instanceof is a preview feature and is disabled by default. (use --enable-preview to enable pattern matching in instanceof)
But I have Java 14 installed.
javac
or the entire error message you're seeing in Eclipse (or whatever else compiler you're using). – Wheelock