E.g. compile the following code:
public class Test {
void test(String s) {
if(s instanceof Object obj) {}
}
}
>"C:\Program Files\Java\jdk-17\bin\javac.exe" --enable-preview --release=17 Test.java
Test.java:3: error: pattern type Object is a subtype of expression type String
if(s instanceof Object obj) {}
^
1 error
Clearly, Object is not a subtype of String. It's vice versa: String is subtype of Object.
public class Test {
void test(String s) {
if(s instanceof Object obj) {}
}
}
>"C:\Program Files\Java\jdk-17\bin\javac.exe" --enable-preview --release=17 Test.java
Test.java:3: error: pattern type Object is a subtype of expression type String
if(s instanceof Object obj) {}
^
1 error
Clearly, Object is not a subtype of String. It's vice versa: String is subtype of Object.
- relates to
-
JDK-8250625 Compiler implementation of Pattern Matching for instanceof (Final)
-
- Resolved
-