For comparing two Doubles better use the #compareTo(Double)
method, it is able to handle NaN
and XXX_INFINITY
in a separated way.
Compares two Double objects numerically. There are two ways in which
comparisons performed by this method differ from those performed by
the Java language numerical comparison operators (<, <=, ==, >=, >)
when applied to primitive double values:
Double.NaN is considered by this method to be equal to itself and
greater than all other double values (including
Double.POSITIVE_INFINITY).
0.0d is considered by this method to be greater than -0.0d. This ensures that the natural ordering of Double objects imposed by this
method is consistent with equals.
public static void main(String[] args) {
Double d = new Double(Double.NaN);
System.out.println(d.compareTo(Double.NaN) == 0);//returns true
}