Saturday, January 31, 2026
HomeLanguagesJavaDouble isNaN() method in Java with examples

Double isNaN() method in Java with examples

The isNaN() method of Java Double class is a built in method in Java returns true if this Double value or the specified double value is Not-a-Number (NaN), or false otherwise.

Syntax:

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

Parameters: The function accepts a single parameter val which specifies the value to be checked when called directly with the Double 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:




// Java code to demonstrate
// Double isNaN() method
// without parameter
  
class GFG {
    public static void main(String[] args)
    {
  
        // first example
        Double f1 = new Double(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 Double(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:




// Java code to demonstrate
// Double isNaN() method
// with parameter
  
class GFG {
    public static void main(String[] args)
    {
  
        // first example
        Double f1 = new Double(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 Double(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()

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
123 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS