Why is an int-to-Object comparison valid in Java 7, but not in Java 8?

Java 7 applies autoboxing to the int.

 private boolean compare(java.lang.Object, int);
   Code:
      0: aload_1
      1: iload_2
      2: invokestatic  #2       // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      5: if_acmpne     12
      8: iconst_1
      9: goto          13
     12: iconst_0
     13: ireturn

I created this with build 1.7.0_71-b14

EDIT:

This behaviour was recognized and treated as bug by Oracle:
JDK-8013357: Javac accepts erroneous binary comparison operations

Relevant JLS section is 15.21. Javac seems to treat this as a
reference comparison, but a reference comparison is only allowed when
BOTH operands are reference types.


The type rules for binary comparisons in JLS Section 15.21 will now be
correctly enforced by javac. Since JDK5, javac has accepted some
programs with Object-primitive comparisons that are incorrectly typed
according to JLS 15.21. These comparisons will now be correctly
identified as type errors.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)