title: How do I compare two Integers?
body: <p>I have to compare two <code>Integer</code> objects (not <code>int</code>). What is the canonical way to compare them?</p>
  
  <pre><code>Integer x = ...
  Integer y = ...
  </code></pre>
  
  <p>I can think of this:</p>
  
  <pre><code>if (x == y) 
  </code></pre>
  
  <p>The <code>==</code> operator only compares references, so this will only work for lower integer values. But perhaps auto-boxing kicks in...?</p>
  
  <pre><code>if (x.equals(y)) 
  </code></pre>
  
  <p>This looks like an expensive operation. Are there any hash codes calculated this way?</p>
  
  <pre><code>if (x.intValue() == y.intValue())
  </code></pre>
  
  <p>A little bit verbose...</p>
  
  <p><strong>EDIT:</strong> Thank you for your responses. Although I know what to do now, the facts are distributed on all of the existing answers (even the deleted ones :)) and I don't really know, which one to accept. So I'll accept the best answer, which refers to all three comparison possibilities, or at least the first two.</p>