title: Does the JVM JIT optimises the creation of new objects that are almost equal?
body: <p>I was reading the source code of the Functional Java library and noticed this:</p>
  <pre><code>public static &lt;T&gt; Option&lt;T&gt; none() {
      return new None&lt;T&gt;();
  }
  </code></pre>
  <p>I was wondering why they don't always return a singleton parameter, specially because of None's equality implementation:</p>
  <pre><code>private static final class None&lt;A&gt; extends Option&lt;A&gt; { 
  
  ...
  
  @Override
  public int hashCode() {
      return 31;
  }
  
  @Override
  public boolean equals(Object obj) {
     if (this == obj)
        return true;
     if (obj == null)
        return false;
     if (getClass() != obj.getClass())
        return false;
     return true;
  }
  
  }
  </code></pre>
  <p>So I did a search at Functional Java's forum and I found this question and answer:</p>
  <blockquote>
  <p>Is it possible to set this up so it doesn't create a new None for every call to none, can we use a single object to represent  None in all cases?</p>
  <p>No, but then, who cares? The JIT optimiser can take care of these things quite well nowadays.</p>
  </blockquote>
  <p>My question is how the JIT optimiser handles this in a way that it isn't necessary to return a singleton. I know that object creation is cheap, but I think a singleton would be cheaper and in this case it doesn't add any complexity.</p>