title: Integer equals method - a question regarding current implementation
body: <p>I'm new to Java, looking at Integer.java implementation, I see the code for 'equals' method is this:</p>
  
  <pre><code>public boolean equals(Object obj) {
          if (obj instanceof Integer) {
              return value == ((Integer)obj).intValue();
          }
          return false;
      }
  </code></pre>
  
  <p>I don't understand why the line </p>
  
  <pre><code>return value == ((Integer)obj).intValue();
  </code></pre>
  
  <p>calls the method intValue(), why can't it directly use the value like this</p>
  
  <pre><code>return value == ((Integer)obj).value;
  </code></pre>
  
  <p>and save a method call. I thought this is aimed to cope with user class which extends Integer. However I see Integer class is declared <strong>final</strong>. So for performance perspective I think the direct use of value is more efficient.</p>
  
  <p>At <a href="https://stackoverflow.com/questions/1195849/in-java-is-there-a-performance-difference-between-referencing-a-field-through-ge">In Java is there a performance difference between referencing a field through getter versus through a variable?</a> there are several answers, some says that using methods vs direct access to variables has minor effect on performance, and other says it has up to 7 times penalty, unless inlined by the compiler. Anyway it has some kind of penalty.</p>
  
  <p>This is not a duplicate of <a href="https://stackoverflow.com/questions/1195849/in-java-is-there-a-performance-difference-between-referencing-a-field-through-ge">In Java is there a performance difference between referencing a field through getter versus through a variable?</a> as
  I'm asking here about the specific code of Integer.java. It seems as a glitch, and the right code shall be </p>
  
  <pre><code>return value == ((Integer)obj).value;
  </code></pre>
  
  <p>What do you think?</p>
  
  <p>BTW, The question is also about the other objects alike Integer e.g Long,Short etc.</p>