• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Pattern Matching with instanceof using strictly subtype

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

This is in reference to the topic Pattern Matching: Subtypes (OCP Java 17 Study guide by Scott and Jeanne: Chapter 3, page 108)

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.

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.



When I try it with my own code, I don't see any error. It just works fine with the output as below.

The output is:
Integers again
both integers

Am I missing something?

Thanks!
 
Master Rancher
Posts: 4661
63
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What version of Java are you using?  There were several "preview" versions of this feature, in Java 14 and Java 15.  But the final version of this feature was in Java 16, and in that version, they added the part about it being a compile error to do an instanceof check using the same type as the variable type - or a supertype of that, for that matter.  So you also can't do

I would note that there's really no point to using Java 14 or 15 at this point - you might as well move to Java 17 at least, since that's an LTS version (Long-Term Support).  And, any time you are using a preview version of a feature (and therefore using the --enable-preview flag while running and compiling) you should remember that those preview features you are experiencing may change their behavior in future releases.
 
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general, you should always use a JDK version that matches the exam you're studying for.
 
Gouri Kalanidhi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using Java 17 version as I'm preparing for OCP 17 certification. Here's the exact version that eclipse IDE is using, extracted from eclipse installation details:


*** Date: Thursday, April 20, 2023 at 11:57:02 AM Central Daylight Time

*** Platform Details:

*** System properties:
java.runtime.name=Java(TM) SE Runtime Environment
java.runtime.version=17.0.6+9-LTS-190

*** System environment variables:

*** Features:

*** Plug-in Registry:

*** User Preferences:

*** Current Install Configuration:

*** Security Configuration:

 
Gouri Kalanidhi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, with the super type added to the original code, I'm not seeing any errors



This prints


Integers again
both integers
this is an object

 
Mike Simmons
Master Rancher
Posts: 4661
63
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, that's very interesting.  I definitely see different behavior here... either there are differences in our specific JDK versions, or one of us is not running the version we think we are.  I'm not using eclipse, rather IntelliJ - but let's take the IDE out of the equation entirely for a moment and use the command line.  I'm using this file, InstanceofPatternMatching.java:

When I compile from the command line:

Can you compile from the command line as well?  What does "javac --version" show?
 
Author
Posts: 249
10
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I concur:


 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simon Roberts wrote:I concur:




You are using the buggy version. Kindly consider https://github.com/openjdk/jdk/commit/739bbd03
 
Mike Simmons
Master Rancher
Posts: 4661
63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like they improved the error message, fixing the identification of which is the "expression type" and which is the "pattern type" - though that doesn't really come up in this example unless you include something like "instanceof Object".  Regardless, even without that bugfix the code still errors out, just with a potentially-misleading message.
 
Gouri Kalanidhi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, so I'm getting compiler errors when I run the same file from command prompt.

\eclipse-workspace\oracle_certified_professional_17\src\ocp_java_17>javac PatternMatching.java
PatternMatching.java:18: error: expression type Integer is a subtype of pattern type Integer
               if (i1 instanceof Integer i2) {
                      ^
PatternMatching.java:21: error: expression type Integer is a subtype of pattern type Integer
               if (i4 instanceof Integer i2) {
                      ^
PatternMatching.java:24: error: expression type Integer is a subtype of pattern type Object
               if (i4 instanceof Object o1) {
                      ^
3 errors




These 2 getProperty() return 17.0.6 and Oracle Corporation respectively, on eclipse.

So, why is eclipse not throwing errors is still not clear. How should I figure if something is really a compiler error and eclipse does not warn?
 
Marshal
Posts: 28009
94
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gouri Kalanidhi wrote:I'm using Java 17 version as I'm preparing for OCP 17 certification. Here's the exact version that eclipse IDE is using, extracted from eclipse installation details:



But the JRE which Eclipse uses is not necessarily the same as the JRE which you have configured into the project's build path.
 
Enthuware Software Support
Posts: 4770
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IDEs are meant to make development easier and they go quite beyond plain javac compiler in reporting errors, hints, and suggestions. IDEs differentiate themselves on this basis.
The certification exam, otoh, is strictly about javac. If you try match the messages from IDEs with javac, you will spend more time than is necessary to get to the point as discovered by so many other candidates: https://enthuware.com/oca-ocp-java-certification-resources/264-preparing-java-certification-ide

 
Bartender
Posts: 5466
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just gave it a try with java 17 and NetBeans 12.5. NetBeans reports that subtype error in my code, and when running I get a warning that the code was compiled with errors, but nevertheless it runs fine. I have the Oracle java version.
 
author & internet detective
Posts: 41775
887
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This gives three compiler errors with Java 17



which are


 
For my next trick, I'll need the help of a tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic