What is Float.isFinite() in Java?
The isFinite() method of the Float class shows whether a Float object is finite.
Syntax
public static boolean isFinite(float val)
Parameter
val: The Float object to be tested.
Return value
It returns true if the argument is a finite floating-point value, and returns false otherwise (for NaN and Inf values).
Code
In the code below, we use the isFinite() method as follows:
class FloatIsFiniteExample {public static void main(String args[]){System.out.println("isFinite(POSITIVE_INFINITY) : " + Float.isFinite(Float.POSITIVE_INFINITY));System.out.println("isFinite(NEGATIVE_INFINITY) : " +Float.isFinite(Float.NEGATIVE_INFINITY));System.out.println("isFinite(NaN) : " +Float.isFinite(Float.NaN));System.out.println("isFinite(1.0f) : " +Float.isFinite(1.0f));}}
Explanation
In the code above,
-
In line 4 we use the
isFinitemethod to check if theFloat.POSITIVE_INFINITYis a finite number. We getfalseas result. -
In line 5 we use the
isFinitemethod to check if theFloat.NEGATIVE_INFINITYis a finite number. We getfalseas result. -
In line 6 we use the
isFinitemethod to check if theFloat.NaNis a finite number. We getfalseas result. -
In line 7 we use the
isFinitemethod to check if the1.0fis a finite number. We gettrueas result.