What is Float.isInfinite() in Java?

Overview

The isInfinite() method of the Float class returns true if the argument is Infinite and returns false for other finite values.

Syntax

public static boolean isInfinite(float val)

Argument

float val: The value to be tested.

Return Value

This method returns true if the argument is either POSITIVE_INFINITY or NEGATIVE_INFINITY. Otherwise, it returns false.

Code

In the below code, we use the isInfinite method as follows:

class FloatIsInfiniteExample {
public static void main(String args[])
{
System.out.println("isInfinite(POSITIVE_INFINITY) : " + Float.isInfinite(Float.POSITIVE_INFINITY));
System.out.println("isInfinite(NEGATIVE_INFINITY) : " +Float.isInfinite(Float.NEGATIVE_INFINITY));
System.out.println("isInfinite(NaN) : " +Float.isInfinite(Float.NaN));
System.out.println("isInfinite(1.0f) : " +Float.isInfinite(1.0f));
}
}

In the above code, we do the following:

  • On line 4, we use the isInfinite method to check if the Float.POSITIVE_INFINITY is an infinite number. We get true as result.

  • On line 5, we use the isInfinite method to check if the Float.NEGATIVE_INFINITY is an infinite number. We get true as result.

  • On line 6, we use the isInfinite method to check if the Float.NaN is an infinite number. We get false as result.

  • On line 7, we use the isInfinite method to check if the 1.0f is an infinite number. We get false as result.

Free Resources