In java we can use the isNan()
method for float and double values.
E.g.:
if (!Double.isNaN(0.01)) {
// condition happens
}
if (!Float.isNaN(0.01F)) {
// condition happens
}
I wonder why we can't use it for Integers.
In java we can use the isNan()
method for float and double values.
E.g.:
if (!Double.isNaN(0.01)) {
// condition happens
}
if (!Float.isNaN(0.01F)) {
// condition happens
}
I wonder why we can't use it for Integers.
NaN is Not a Number, i.e., an undefined or unrepresentible number, such as the square root of -1. Integers are always well defined, and Strings just aren't numbers at all, so they are irrelevant for checking against being NaN.
NaN
is a special value of floating point numbers, along with others like Infinity
; integers and strings don't have such special values, so the functions to check for them would make no sense.
The reason they exist is to represent the results of certain calculations, so that floating point numbers can be used in arbitrary mathematical situations.