Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2" rel="nofollow noreferrer">Per the JLS</a>, <code>==</code> comparisons between a boxed and an unboxed value result in an <em>unboxing</em> conversion, not the other way around (or else you'd be using reference equality, not value equality). The compiler can't unbox a plain <code>Number</code>; <code>Number</code> itself isn't "convertible to <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2" rel="nofollow noreferrer">a numeric type</a>."</p> <p>The Java 7 compiler appears to be being clever. The assembly output for this version and for the version in which the comparison is moved into a <code>private</code> method completely ignore the declared type <code>Number</code>, and everything works properly. Make that method <code>public</code>, and the behavior is as specified: The conversion isn't one of the listed types for which unboxing will occur, and the compiler boxes <code>10</code> to an <code>Integer</code> and compares by reference, meaning that if you try using <code>Integer.valueOf(10)</code>, you'll get <code>true</code> for the range -128..127, and if you use anything else (another width, <code>new Integer(10)</code>), you'll get <code>false</code>.</p> <p>Output from your code (note that <code>Number</code> appears nowhere, and that you're getting comparison based on reference equality in line 18; try using <code>L</code> or casting to <code>short</code>):</p> <pre><code>public static void main(java.lang.String[]); Code: 0: bipush 10 2: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 5: astore_1 6: bipush 10 8: istore_2 9: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream; 12: aload_1 13: bipush 10 15: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 18: if_acmpne 25 21: iconst_1 22: goto 26 25: iconst_0 26: invokevirtual #4 // Method java/io/PrintStream.println:(Z)V 29: return </code></pre> <p>Version that prevents optimization:</p> <pre><code>public class Test { public static void main(String[] args) { Number n = new Integer(10); compare(n); } public static void compare(Number n) { int i=10; System.out.println(n == 10); } } </code></pre> <p>Assembly; note that you're still getting a reference comparison in line 12:</p> <pre><code>public static void main(java.lang.String[]); Code: 0: new #2 // class java/lang/Integer 3: dup 4: bipush 10 6: invokespecial #3 // Method java/lang/Integer."&lt;init&gt;":(I)V 9: astore_1 10: aload_1 11: invokestatic #4 // Method compare:(Ljava/lang/Number;)V 14: return public static void compare(java.lang.Number); Code: 0: bipush 10 2: istore_1 3: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 6: aload_0 7: bipush 10 9: invokestatic #6 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 12: if_acmpne 19 15: iconst_1 16: goto 20 19: iconst_0 20: invokevirtual #7 // Method java/io/PrintStream.println:(Z)V 23: return </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload