Java - Float Class isInfinite() Method

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

Syntax

public boolean isInfinite ();
public static boolean isInfinite (float value);

Float class isInfinite() method

  • isInfinite() method is available in java.lang package.
  • isInfinite() method is used to check the infinity of this Float object (i.e. either positive infinity or negative infinity).
  • isInfinite(float value) method is used to check the infinity of the given argument is of float type (i.e. either positive infinity or negative infinity).
  • These are non-static methods, they are accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • These methods do not throw an exception at the time of checking infinity.

Parameters

  • In the First case – isInfinite(), we don't pass any parameter or value.
  • In the Second case – isInfinite(float value), we pass only one parameter or value is of float type.

Return Value

The return type of this method is boolean, it returns a boolean value either true or false depending on the condition.

  • In the first case, if the given value represented by the object is either positive infinity or negative infinity, it returns true.
  • In the second case, if the given value represented by the object is not either positive infinity or negative infinity, it returns false.

Example

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

public class IsInfiniteOfFloatClass {
    public static void main(String[] args) {
        // Object initialization
        Float ob1 = new Float(10.0 / 0.0);
        Float ob2 = new Float(-20.0 / 0.0);
        Float ob3 = new Float(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  

        boolean infinite1 = ob1.isInfinite();
        boolean infinite2 = ob2.isInfinite();

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

        // It checks infinity by calling Float.isInfinite(ob3) for ob3
        boolean infinite3 = Float.isInfinite(ob3);

        // Display result values
        System.out.println("Float.isInfinite(ob3): " + infinite3);
    }
}

Output

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

Comments and Discussions!

Load comments ↻





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