Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

float
java
communitycreator

What is Float.isFinite() in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

float
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring