instanceof with primitive type

Ella Ananeva ella.ananeva at oracle.com
Wed Nov 8 18:18:19 UTC 2023


Hi team,
Reading JEP 455 spec<https://cr.openjdk.org/~abimpoudis/instanceof/jep455-20231030/specs/instanceof-jls.html#jls-15.20.2>, I see this:

The type of the expression RelationalExpression can be a reference type, a primitive type or the null type.
And then, later:

The following rules apply when instanceof is the pattern match operator:

  *   The type of the expression RelationalExpression must be a reference type or the null type, or a compile-time error occurs.

But I would expect that primitive types in patterns should be good, too. And in the freshest version of JEP_455 JDK<https://mach5.us.oracle.com/mdash/buildIds/2023-11-06-1633093.angelos.bimpoudis.dev>, both examples compile and print “Yes”:

Integer x = 5;
if (x instanceof int y) System.out.println("Yes");

int x = 5;
if (x instanceof Integer y) System.out.println("Yes");

Shouldn’t the spec be updated?

Thank you,
Ella Ananeva
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20231108/ae7656a5/attachment-0001.htm>


More information about the amber-dev mailing list