Tag Archives: equals

Double.equals pitfalls

It turns out that Double.equals is not always the same as == with two double primitives. Case in point:
double a = Double.NaN;
System.out.println(a == a);
Double b = new Double(a);
System.out.println(b.equals(b));
This code will print out false and true.
What’s going on? Basically, if you look at the Javadoc for the equals method, it says that “two double values are [...]