title: Why does Double.NaN equal itself when wrapped in a Double instance?
body: <p>From <a href="https://stackoverflow.com/questions/471296/how-can-while-i-i-be-a-non-infinite-loop">this question</a> I learned Double.NaN is not equal to itself.</p>
  
  <p>I was verifying this for myself and noticed this is not the case if you wrap Double.NaN in a Double instance. For example:</p>
  
  <pre><code>public class DoubleNaNTest {
      public static void main(String[] args) {
          double primitive = Double.NaN;
          Double object = new Double(primitive);
  
          // test 1 - is the primitive is equal to itself?
          boolean test1 = primitive == primitive;
  
          // test 2 - is the object equal to itself?
          boolean test2 = object.equals(object);
  
          // test 3 - is the double value of the object equal to itself?
          boolean test3 = object.doubleValue() == object.doubleValue();
  
          System.out.println("Test 1 = " + test1);
          System.out.println("Test 2 = " + test2);
          System.out.println("Test 3 = " + test3);
      }
  }
  </code></pre>
  
  <p>Outputs:</p>
  
  <pre><code>Test 1 = false
  Test 2 = true
  Test 3 = false
  </code></pre>
  
  <p>It seems to me that all three tests should evaluate to false as all three operations are equivalent (as they are if you use something other then Double.NaN).</p>
  
  <p>Could someone explain what's going on here?</p>