从这个问题我学到了Double.NaN不等于它自己.
我正在为自己验证这一点,并注意到如果你在Double实例中包装Double.NaN则不是这种情况.例如:
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); …Run Code Online (Sandbox Code Playgroud) java ×1