Java - Double Class isInfinite() Method

Double class isInfinite() method: Here, we are going to learn about the isInfinite() method of Double class with its syntax and example. By Preeti Jain Last updated : March 23, 2024

Double class isInfinite() method

  • isInfinite() method is available in Double class of java.lang package.
  • isInfinite() method is used to check infinity (i.e. either positive infinity or negative infinity).
  • isInfinite() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • isInfinite() method does not throw an exception at the time of checking infinity.

Syntax

public boolean isInfinite();

Parameters

  • It does not accept any parameter.

Return Value

The return type of this method is boolean, it returns "true" if this object is either positive or negative infinity, else it returns "false".

Example

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

public class IsInfiniteOfDoubleClass {
    public static void main(String[] args) {
        // Object initialization
        Double ob1 = new Double(10.0 / 0.0);
        Double ob2 = new Double(-20.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 infinity by calling ob1.isInfinite() for ob1
        // and ob2.isInfinite() for ob2 and ob2.isInfinite() for ob3 
        boolean infinite1 = ob1.isInfinite();
        boolean infinite2 = ob2.isInfinite();
        boolean infinite3 = ob3.isInfinite();

        // Display result values
        System.out.println("ob1.isInfinite(): " + infinite1);
        System.out.println("ob2.isInfinite(): " + infinite2);
        System.out.println("ob3.isInfinite(): " + infinite3);
    }
}

Output

ob1: Infinity
ob2: -Infinity
ob3: 20.0
ob1.isInfinite(): true
ob2.isInfinite(): true
ob3.isInfinite(): false

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.