In Java, there can be situations where there is a need to refrain from the βNaNβ values. More specifically, while dealing with garbage values or mathematical computations accumulating unnecessary memory. In such cases, βNaNβ in Java is of great aid in checking for a valid number and managing the memory effectively.
This article will demonstrate the usage and occurrence of βNaNβ in Java.
What is βNaNβ in Java?
βNaN(Not a Number)β in Java is a special floating point value referring to errors. It usually corresponds to the outcome of invalid operations. For example, dividing zero by zero, dividing a floating point number by zero, or taking the square root of a negative number, etc.
Example 1: Operations Leading to βNaNβ in Java
In this example, the computations resulting in the βNaNβ outcome can be implemented:
public static void main(String[] args) {
System.out.println("Zero by Zero: "+0.0 / 0.0);
System.out.println("Mod by Zero: "+10.0 % 0);
System.out.println("Square Root of -1: " + Math.sqrt(-1));
System.out.println("Log of -1: " + Math.log(-1));
}}
In the above lines of code, divide zero by zero, take βmodβ by zero, and return the βsquare rootβ and βlogβ of a negative number, respectively. All of these computations lead to a βNaNβ outcome.
Output
In the above output, it can be seen that all of the calculations yield βNaNβ, as a result.
Example 2: Comparing βNaNβ Values in Java
In this particular example, the βNaNβ values corresponding to various classes can be compared. Before proceeding to the example, consider the following points:
-
- The comparison operators β<β, β<=β, β>β, and β>=β always return βfalseβ if either or both the operands are βNaNβ.
- The equality operator β==β returns βfalseβ if either of the operands is NaN.
- The inequality operator β!=β returns βtrueβ if either of the operands is NaN .
Now, letβs move on to the following lines of code:
public static void main(String[] args) {
System.out.println (Float.NaN != Float.NaN);
System.out.println (Float.NaN == Float.NaN);
System.out.println (Float.NaN < Float.NaN);
System.out.println (Float.NaN <= Float.NaN);
System.out.println (Float.NaN >= Float.NaN);
System.out.println (Double.NaN != Double.NaN);
System.out.println (Double.NaN == Double.NaN);
System.out.println (Double.NaN > Double.NaN);
System.out.println (Double.NaN <= Double.NaN);
System.out.println (Double.NaN >= Double.NaN);
}}
In this code snippet, specify βFloat.NaNβ and βDouble.NaNβ for the constant fields in both classes, respectively to perform the comparisons. Note that the comparison is done based on the discussed specified conditions and the corresponding boolean outcome is returned.
Output
In this outcome, it can be analyzed that only the comparison against the β!=β operator returned the outcome βtrueβ in both classes accordingly.
Conclusion
βNan(Not a Number)β in Java usually corresponds to the outcome of invalid operations like dividing zero by zero, dividing a floating point number by zero, or taking the square root of a negative number, etc. This blog discussed the usage and implementation of βNaNβ in Java.