Java.lang Package Classes

Java Float - isNaN() Method



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

Syntax

public boolean isNaN()

Parameters

No parameter is required.

Return Value

Returns true if the value represented by this object is NaN; false otherwise.

Exception

NA.

Example:

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

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating Float value
    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?: " + x.isNaN());  
    System.out.println("Is y NaN?: " + y.isNaN()); 
    System.out.println("Is z NaN?: " + z.isNaN());  
  }
}

The output of the above code will be:

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

❮ Java.lang - Float