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
isInfinitemethod to check if theFloat.POSITIVE_INFINITYis an infinite number. We gettrueas result. -
On line 5, we use the
isInfinitemethod to check if theFloat.NEGATIVE_INFINITYis an infinite number. We gettrueas result. -
On line 6, we use the
isInfinitemethod to check if theFloat.NaNis an infinite number. We getfalseas result. -
On line 7, we use the
isInfinitemethod to check if the1.0fis an infinite number. We getfalseas result.