Home » Java programming language

Java Double class isNaN() method with example

Double class isNaN() method: Here, we are going to learn about the isNaN() method of Double class with its syntax and example.
Submitted by Preeti Jain, on September 22, 2019

Syntax:

    public boolean isNaN ();
    public static boolean isNaN(double value);

Double class isNaN() method

  • isNaN() method is available in java.lang package.
  • isNaN() method is used to check NaN (Not a Number) values (i.e. either positive NaN or negative NaN).
  • isNaN(double value) method is used to check NaN values for the given double argument (i.e. either positive or negative NaN).
  • isNaN() method does not throw an exception at the time of checking NaN values represented by the object.
  • Similarly, isNaN(double value) method does not throw an exception at the time of checking NaN values of the given argument.
  • Both types of methods are the non-static methods, they are accessible with the class object only and if we try to access them with the class name then we will get an error.

Parameter(s):

  • In the first case isNaN(), we don't pass any parameter or value.
  • In the second case isNaN(double value), we pass only one parameter of double type, it represents the double value to be tested for NaN.

Return value:

The return type of this method is boolean, it returns wither "true" if the value if either positive or negative NaN, it returns "false" if the value is not NaN.

Example:

// Java program to demonstrate the example 
// of isNaN() method of Double class

public class IsNaNOfDoubleClass {
    public static void main(String[] args) {
        // Object initialization
        Double ob1 = new Double(0.0 / 0.0);
        Double ob2 = new Double(-0.0 / 0.0);
        Double ob3 = new Double(20.0);

        // Display ob1,ob2 and ob3 values
        System.out.println("ob1: " + ob1);
        System.out.println("ob2: " + ob2);
        System.out.println("ob3: " + ob3);

        // It checks NaN by calling ob1.isNaN() for ob1
        // and ob2.isNaN() for ob2  
        boolean NaN1 = ob1.isNaN();
        boolean NaN2 = ob2.isNaN();

        // It also checks NaN of this Double object by calling
        // ob3.isNaN(ob3) for ob3 
        boolean NOTNaN = ob3.isNaN(ob3);

        // Display result values
        System.out.println("ob1.isNaN(): " + NaN1);
        System.out.println("ob2.isNaN(): " + NaN2);
        System.out.println("ob3.isNaN(ob3): " + NOTNaN);
    }
}

Output

ob1: NaN
ob2: NaN
ob3: 20.0
ob1.isNaN(): true
ob2.isNaN(): true
ob3.isNaN(ob3): false






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.