Java.lang Package Classes

Java Float - isNaN() Method



The java.lang.Float.isNaN() method is used to check whether the specified number is a Not-a-Number (NaN) or not. The method returns true if the specified number is a Not-a-Number (NaN), false otherwise.

Syntax

public static boolean isNaN(float v)

Parameters

v Specify the value to be tested.

Return Value

Returns true if the argument is NaN; false otherwise.

Exception

NA.

Example:

In the example below, the java.lang.Float.isNaN() method is used to check whether the given number is NaN or not.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating float values
    float x = 5.2f;
    float y = 0.0f/0.0f;
    float z = Float.NaN;

    //checking float values for NaN
    System.out.println("Is x NaN?: " + Float.isNaN(x));  
    System.out.println("Is y NaN?: " + Float.isNaN(y)); 
    System.out.println("Is z NaN?: " + Float.isNaN(z));  
  }
}

The output of the above code will be:

Is x NaN?: false
Is y NaN?: true
Is z NaN?: true

❮ Java.lang - Float