Skip to content
Related Articles

Related Articles

Float isNaN() method in Java with examples
  • Last Updated : 26 Oct, 2018

The Float.isNaN() method in Float Class is a built in method in Java returns true if this Float value or the specified float value is Not-a-Number (NaN), or false otherwise.

Syntax:

public boolean isNaN()
        or
public static boolean isNaN(float val)

Parameters: he function accepts a single parameter val which specifies the value to be checked when called directly with the Float class as static method. The parameter is not required when the method is used as instance method.

Return Value: It returns true if the val is NaN else it return false.

Below programs illustrate isNaN() method in Java:



Program 1: Using static isNaN() method




// Java code to demonstrate
// Float isNaN() method
// without parameter
  
class GFG {
    public static void main(String[] args)
    {
  
        // first example
        Float f1 = new Float(1.0 / 0.0);
  
        boolean res = f1.isNaN();
  
        // printing the output
        if (res)
            System.out.println(f1 + " is NaN");
        else
            System.out.println(f1 + " is not NaN");
  
        // second example
        f1 = new Float(0.0 / 0.0);
  
        res = f1.isNaN();
  
        // printing the output
        if (res)
            System.out.println(f1 + " is NaN");
        else
            System.out.println(f1 + " is not NaN");
    }
}
Output:
Infinity is not NaN
NaN is NaN

Program 2: Using non-static isNaN() method




// Java code to demonstrate
// Float isNaN() method
// with parameter
  
class GFG {
    public static void main(String[] args)
    {
  
        // first example
        Float f1 = new Float(1.0 / 0.0);
  
        boolean res = f1.isNaN(f1);
  
        // printing the output
        if (res)
            System.out.println(f1 + " is NaN");
        else
            System.out.println(f1 + " is not NaN");
  
        // second example
        f1 = new Float(0.0 / 0.0);
  
        res = f1.isNaN(f1);
  
        // printing the output
        if (res)
            System.out.println(f1 + " is NaN");
        else
            System.out.println(f1 + " is not NaN");
    }
}
Output:
Infinity is not NaN
NaN is NaN

Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#isNaN()

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :