Jeanne Boyarsky wrote:
Crystal Kirscht wrote:I discovered the same thing. Only it only compiles in Eclipse and in VSCode (which both use Java 17). However when I run Java from the command line on the same code it does not compile. I'm not sure what is different
Can you type
My guess is your command line is using Java 21 and triggering the change in behavior Paul mentioned.
It is actually on the command line that it is NOT compiling.
D:\MyWorkspace\HelloWorld\src\chapter3>javac PatternMatching.java
PatternMatching.java:38: error: expression type Integer is a subtype of pattern type Integer
if(value instanceof Integer data) {
^
1 error
D:\MyWorkspace\HelloWorld\src\chapter3>java --version
java 17.0.4.1 2022-08-18 LTS
Java(TM) SE Runtime Environment (build 17.0.4.1+1-LTS-2)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.4.1+1-LTS-2, mixed mode, sharing)
It is in Eclipse and VSCode where it IS compiling. But I know those are using Java 17 as I have not installed any other versions and I have checked the settings in Eclipse (I'm not sure where they are in VSCode). I've attached screenshots of the code and jre settings in eclipse