title: When using == for a primitive and a boxed value, is autoboxing done, or is unboxing done
body: <p>The following code compiles (with Java 8):</p>
  
  <pre><code>Integer i1 = 1000;
  int i2 = 1000;
  boolean compared = (i1 == i2);
  </code></pre>
  
  <p>But what does it do?</p>
  
  <p>Unbox <code>i1</code>:</p>
  
  <pre><code>boolean compared = (i1.intvalue() == i2);
  </code></pre>
  
  <p>or box <code>i2</code>:</p>
  
  <pre><code>boolean compared = (i1 == new Integer(i2));
  </code></pre>
  
  <p>So does it compare two <code>Integer</code> objects (by reference) or two <code>int</code> variables by value?</p>
  
  <p>Note that for some numbers the reference comparison will yield the correct result because the Integer class maintains an internal cache of values between <code>-128</code> to <code>127</code> (see also the comment by TheLostMind). This is why I used <code>1000</code> in my example and why I specifically ask about the unboxing/boxing and not about the result of the comparison.</p>