About Question enthuware.ocpjp.v17.-2-.3719 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Tester
Posts: 33
Joined: Mon Oct 30, 2023 11:55 am
Contact:

About Question enthuware.ocpjp.v17.-2-.3719 :

Post by Tester »

Could you please explain:
So, (b instanceof A a) will not compile

Code: Select all

class A {}
class B extends A { }

    public static void main(String args[]) {
      B b = new B();
      if((b instanceof A a)) {
        System.out.println("Works");
      }
    }

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v17.2.3719 :

Post by admin »

The explanation already explains the reason:
So, (b instanceof A a) will not compile because the compiler knows that b will always point to an instanceof A and this check is pointless. This is a new feature of instanceof that is applicable only when used with pattern matching.
Can you tell me which part of this explanation is not clear?

Tester
Posts: 33
Joined: Mon Oct 30, 2023 11:55 am
Contact:

Re: About Question enthuware.ocpjp.v17.2.3719 :

Post by Tester »

Is the code I write correct? Is it about your example? If Yes, please explain, why it compile? If Not, what is wrong?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v17.2.3719 :

Post by admin »

No, the code that you have written above doesn't compile. You can try it out on your machine also. The error message is "error: expression type B is a subtype of pattern type A if(b instanceof A a) {" and the reason is the same that is given.

So I still don't understand what is your confusion.

Tester
Posts: 33
Joined: Mon Oct 30, 2023 11:55 am
Contact:

Re: About Question enthuware.ocpjp.v17.-2-.3719 :

Post by Tester »

please correct me if I do anything wrong.
file: A.java

Code: Select all

public class A {}
file: B.java

Code: Select all

public class B extends A {}
file: C.java

Code: Select all

public class C {
    public static void main(String args[]) {
      B b = new B();
      if((b instanceof A a)) {
        System.out.println("Works");
      }
    }
}
I run C and get "Works". I did the same as inner classes in one file and result is the same.
By the way, I am using Java Version 17, not sure about later version.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v17.-2-.3719 :

Post by admin »

Not sure how you are compiling but it doesn't compile on Java 17. The error is the same as I quoted above.

Tester
Posts: 33
Joined: Mon Oct 30, 2023 11:55 am
Contact:

Re: About Question enthuware.ocpjp.v17.-2-.3719 :

Post by Tester »

Sorry, I have no idea how to explain.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v17.-2-.3719 :

Post by admin »

I think Java changed the behavior because I compiled it with JDK 17.0.3 and it failed to compile. I then compiled it with Java 21 and it compiled without any error. Something changed between the versions.

So I will need to dig into it further to find out what is going on.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v17.-2-.3719 :

Post by admin »

As per JLS 17, section 15.20.2 (page 662):
If the type of the RelationalExpression is a subtype of the type of the Pattern, then a compile-time error occurs.
As per this rule, it should not compile.

However, this rule is not there in JLS 21.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v17.-2-.3719 :

Post by admin »

This restriction was removed in Java 19.
So, you are most likely using a different javac version to compile your code.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests