Java

NaN in Java

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 class nan {
 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 class nan2 {
 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.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.