title: Effective java item 9: overriding hashcode example
body: <p>I was reading Effective Java Item 9 and decided to run the example code by myself. But it works slightly different depending on how I insert a new object that I don't understand what exactly is going on inside. The PhoneNumber class looks: </p>
  
  <pre><code>public class PhoneNumber {
  
  private final short areaCode;
  private final short prefix;
  private final short lineNumber;
  
  public PhoneNumber(int areaCode, int prefix, int lineNumber) {
      this.areaCode = (short)areaCode;
      this.prefix = (short) prefix;
      this.lineNumber = (short)lineNumber;
  }
  
  @Override public boolean equals(Object o) {
      if(o == this) return true;
      if(!(o instanceof PhoneNumber)) return false;
      PhoneNumber pn = (PhoneNumber)o;
      return pn.lineNumber == lineNumber &amp;&amp; pn.prefix == prefix &amp;&amp; pn.areaCode == areaCode;
  }
  }
  </code></pre>
  
  <p>Then according to the book and as is when I tried, </p>
  
  <pre><code>    public static void main(String[] args) {
  
           HashMap&lt;PhoneNumber, String&gt; phoneBook = new HashMap&lt;PhoneNumber, String&gt;();
           phoneBook.put(new PhoneNumber(707,867,5309), "Jenny");
           System.out.println(phoneBook.get(new PhoneNumber(707,867,5309)));
      }
  </code></pre>
  
  <p>This prints "null" and it's explained in the book because HashMap has an optimization that caches the hash code associated with each entry and doesn't check for object equality if the hash codes don't match. It makes sense to me. But when I do this:</p>
  
  <pre><code>    public static void main(String[] args) {
  
           PhoneNumber p1 = new PhoneNumber(707,867,5309);
           phoneBook.put(p1, "Jenny");
           System.out.println(phoneBook.get(new PhoneNumber(707,867,5309)));
      }
  </code></pre>
  
  <p>Now it returns "Jenny". Can you explain why it didn't fail in the second case? </p>