hg: jdk7/tl/langtools: 6979683: inconsistent interaction of reference cast with box/unbox conversions leaves out a useful case

Alex Buckley alex.buckley at oracle.com
Wed Sep 11 11:43:59 PDT 2013


Please be more specific than "latest JDK". In the JDK 8 Developer 
Preview, the 3==x comparison is rejected with the message that int and 
Object are incomparable types. That is consistent with JLS7 15.21.

Now, this topic has some history. It used to be that javac was sensitive 
to the order of the operands; see JDK-6526449. I happen to agree with 
you that == should accept a mix of primitive and reference types, 
provided that casting conversion can convert one type to the other. 
However, it didn't happen in SE 7 (hence 3==x is rejected) and we're not 
revisiting it now.

Alex

On 9/11/2013 9:29 AM, Hollis Waite wrote:
> Since Java 7 allows implicit chained casts, should it be legal to compare primitives with Objects via the '==' operator? JLS 15.21 states:
>
>
>    The equality operators may be used to compare two operands that
>    are convertible (§5.1.8) to numeric type, or two operands of
>    type boolean or Boolean, or two operands that are each of either
>    reference type or the null type. All other cases result in a
>    compile-time error.
>
> However, latest JDK accepts the below code:
>
>    Object x = "";
>    boolean a = (3 == x);
>
> It seems to me that the new '=' guidelines are inconsistent with official (but not *actual*) behavior of '=='. Should JLS 15.21 be updated?
>


More information about the compiler-dev mailing list