LAST UPDATED: JANUARY 15, 2021
Java Float isInfinite() Method
Java isInfinite()
method is a part of the Float
class of the java.lang
package. This method is the exact opposite of the isFinite()
method and is used to check whether the passed floating value is an infinite floating-point value or not. It returns the boolean value false
for finite floating values and true
for NaN and infinite values.
Syntax:
public static boolean isInfinite()
Parameters:
No parameters are passed in this method.
Returns:
Returns false
if the passed floating value has a finite value and returns true
for infinite and NaN values.
Example 1:
Here, a boolean value is returned in accordance with the float value passed as an argument.
import java.lang.Float;
public class StudyTonight
{
public static void main(String[] args)
{
Float f1 = 528648.67f;
Float f2 = f1/0.0f;
Float f3 = -f1/0.0f;
Float f4 = 0.0f/0.0f;
System.out.println("The value is : " +f1.isInfinite()); //returns false for finite value
System.out.println("The value is : " +f2.isInfinite()); //returns true for infinite value
System.out.println("The value is : " +f3.isInfinite()); //returns true for infinite value
System.out.println("The value is : " +f4.isInfinite()); // returns false for finite value
}
}
The value is : false
The value is : true
The value is : true
The value is : false
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.
import java.lang.Float;
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.println("Enter the value");
Scanner sc = new Scanner(System.in);
float f = sc.nextFloat();
Float i = f;
boolean b = i.isInfinite();
if(b== true)
{
System.out.println("Value is infinite");
}
else
{
System.out.println("Value is finite");
}
}
catch(Exception e)
{
System.out.println("Invalid Input");
}
}
}
Enter the value
789E85689
Value is infinite
*******************
Enter the value
523.87
Value is finite
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.