Page 1 of 1

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

Posted: Mon Nov 20, 2023 5:48 pm
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");
      }
    }

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

Posted: Tue Nov 21, 2023 5:38 am
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?

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

Posted: Tue Nov 21, 2023 7:24 am
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?

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

Posted: Tue Nov 21, 2023 8:23 am
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.

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

Posted: Tue Nov 21, 2023 10:50 am
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.

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

Posted: Wed Nov 22, 2023 6:57 am
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.

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

Posted: Wed Nov 22, 2023 7:31 am
by Tester
Sorry, I have no idea how to explain.

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

Posted: Wed Nov 22, 2023 10:52 am
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.

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

Posted: Wed Nov 22, 2023 10:58 am
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.

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

Posted: Wed Nov 22, 2023 11:24 pm
by admin
This restriction was removed in Java 19.
So, you are most likely using a different javac version to compile your code.