Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you compile a number literal in Java and assign it to a Integer (capital <code>I</code>) the compiler emits:</p> <pre><code>Integer b2 =Integer.valueOf(127) </code></pre> <p>This line of code is also generated when you use autoboxing.</p> <p><code>valueOf</code> is implemented such that certain numbers are "pooled", and it returns the same instance for values smaller than 128.</p> <p>From the java 1.6 source code, line 621:</p> <pre><code>public static Integer valueOf(int i) { if(i &gt;= -128 &amp;&amp; i &lt;= IntegerCache.high) return IntegerCache.cache[i + 128]; else return new Integer(i); } </code></pre> <p>The value of <code>high</code> can be configured to another value, with the system property.</p> <blockquote> <p>-Djava.lang.Integer.IntegerCache.high=999</p> </blockquote> <p>If you run your program with that system property, it will output true!</p> <p>The obvious conclusion: never rely on two references being identical, always compare them with <code>.equals()</code> method.</p> <p>So <code>b2.equals(b3)</code> will print true for all logically equal values of b2,b3.</p> <p>Note that Integer cache is not there for performance reasons, but rather to comform to the <a href="http://java.sun.com/docs/books/jls/third%5Fedition/html/conversions.html#5.1.7" rel="noreferrer">JLS, section 5.1.7</a>; object identity must be given for values -128 to 127 inclusive.</p> <p><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf%28int%29" rel="noreferrer">Integer#valueOf(int)</a> also documents this behavior:</p> <blockquote> <p>this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    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