title: What should I use as a hash code of null?
body: <p>Assume we have a following trivial class:</p>
  
  <pre><code>public class Foo {
  
      public Integer bar;
  
  }
  </code></pre>
  
  <p>And we want to build a "good" <code>hashCode</code> method for it. By "good" I would, for instance mean that there's a small probability of hash code collision in "real-life" cases.</p>
  
  <p>In "real-life" for such a class I would reasonably expect <code>Foo</code>s with <code>bar</code> set to <code>null</code> or <code>0</code>. I'd even argue that these two are probably the most frequent values.</p>
  
  <p>But let's take a look at what Eclipse, for instance, generates:</p>
  
  <pre><code>public class Foo {
  
      public Integer bar;
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((bar == null) ? 0 : bar.hashCode());
          return result;
      }
  }
  </code></pre>
  
  <p>And it's not just Eclipse, it seems like using <code>0</code> as <code>hashCode</code> for <code>null</code> is a normal practice.</p>
  
  <p>But this would produce identical hash codes for <code>null</code> and <code>0</code>, wouldn't it? An as I'm assuming that <code>null</code> and <code>0</code> are probably the most frequent cases - this leads to a higher probabiliy of collission.</p>
  
  <p>So here comes my question. <strong>What would be a good <code>hashCode</code> value for <code>null</code>?</strong></p>