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 isFinite method to check if the Float.POSITIVE_INFINITY is a finite number. We get false as result.

  • In line 5 we use the isFinite method to check if the Float.NEGATIVE_INFINITY is a finite number. We get false as result.

  • In line 6 we use the isFinite method to check if the Float.NaN is a finite number. We get false as result.

  • In line 7 we use the isFinite method to check if the 1.0f is a finite number. We get true as result.

Free Resources