title: Why would one want to use the public constructors on Boolean and similar immutable classes?
body: <p>(For the purposes of this question, let us assume that one is intentionally not using auto(un)boxing, either because one is writing pre-Java 1.5 code, or because one feels that autounboxing makes it too easy to create <code>NullPointerException</code>s.)</p>
  
  <p>Take <code>Boolean</code>, for example. The documentation for the <code>Boolean(boolean)</code> constructor says:</p>
  
  <blockquote>
    <p>Note: It is rarely appropriate to use this constructor. Unless a <em>new</em>
    instance is required, the static factory <code>valueOf(boolean)</code> is generally
    a better choice. It is likely to yield significantly better space and time
    performance.</p>
  </blockquote>
  
  <p>My question is, why would you <em>ever</em> want to get a new instance in the first place? It seems like things would be simpler if constructors like that were private. For example, if they were, you could write this with no danger (even if <code>myBoolean</code> were <code>null</code>):</p>
  
  <pre><code>if (myBoolean == Boolean.TRUE)
  </code></pre>
  
  <p>It'd be safe because all true <code>Booleans</code> would be references to <code>Boolean.TRUE</code> and all false <code>Booleans</code> would be references to <code>Boolean.FALSE</code>. But because the constructors are public, someone may have used them, which means that you have to write this instead:</p>
  
  <pre><code>if (Boolean.TRUE.equals(myBoolean))
  </code></pre>
  
  <p>But where it really gets bad is when you want to check two <code>Booleans</code> for equality. Something like this:</p>
  
  <pre><code>if (myBooleanA == myBooleanB)
  </code></pre>
  
  <p>...becomes this:</p>
  
  <pre><code>if (
      myBooleanA == myBooleanB ||
      (myBooleanA != null &amp;&amp; myBooleanA.equals(myBooleanB))
  )
  </code></pre>
  
  <p>UPDATE: With the release of Java 7, java.util.Objects makes this simpler construct possible:</p>
  
  <pre><code>if (Objects.equals(myBooleanA, myBooleanB))
  </code></pre>
  
  <p>I can't think of any reason to have separate instances of these objects which is more compelling than not having to do the nonsense above. What say you?</p>