This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
You can also use Double.isNaN() to find out if a double is a NaN. Similarly Float.isNaN() for floats. That might be slightly clearer to programmers who forget about the existence of NaN.
Swati, Sandip - the reason NaN != NaN is by design. What should happen here?
Here two different calculations have the result NaN - should they be equal to each other? Probably not. They're both NaN, but they're different. Even if both x and y were 1.0/ 0.0, the word "equals" doesn't really have any clear meaning when you're talking about a number that is not a number. Equal to what? It's a special case, and Java has special rules for it. So do several other languages, since the idea of NaN existed before Java.