使用Double.isNaN方法引发的问题
想要判断一个double是不是NaN,以下是JDK源码:
/**
* Returns {@code true} if the specified number is a
* Not-a-Number (NaN) value, {@code false} otherwise.
*
* @param v the value to be tested.
* @return {@code true} if the value of the argument is NaN;
* {@code false} otherwise.
*/
public static boolean isNaN(double v) {
return (v != v);
}
由于粗心,传给这个方法的类型是Double对象,所以会自动拆箱。
碰巧的是调用方传过来的这个Double类型的参数是null,导致程序报错。
因为自动拆箱会调用null.doubleValue()