Fabio Yasuo Yamashita wrote:Baiscally the book says the pattern variable must be a subtype of the variable on the left side of the expression. It cannot be same.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
The type of the pattern variable must be a subtype of the variable on the left side of the
expression. It also cannot be the same type. This rule does not exist for traditional instanceof
operator expressions, though. Consider the following two uses of the instanceof operator:
While the second line compiles, the last line does not compile because pattern matching
requires that the pattern variable type Integer be a strict subtype of Integer .
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
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
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
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.
Fabio Yasuo Yamashita wrote:
It works
Campbell Ritchie wrote:Which version of Java® are you using?