Introduction
In Java, the Double class is used to represent double-precision floating-point numbers. It provides various methods to manipulate double values. The isInfinite() method of the Double class is used to check if a double value is infinite or not.
Create a Java Program File
First, create a Java file named DoubleIsInfinite.java in the ~/project directory using the following command:
touch ~/project/DoubleIsInfinite.java
Then, open the file in a text editor using the following command:
touch ~/project/DoubleIsInfinite.java
Declare a double value
Declare a double value, for example, number, and initialize it with a value of your choice.
double number = 67.98;
Check if the value is infinite
Use the isInfinite() method to check if the number value is infinite or not.
if(Double.isInfinite(number)){
System.out.println("The number is infinite.");
} else {
System.out.println("The number is not infinite.");
}
Compile and run the code
Save the changes on the file DoubleIsInfinite.java and exit the text editor. Then, compile and run the code using the following commands:
javac ~/project/DoubleIsInfinite.java && java DoubleIsInfinite
Test the code
Test the code by changing the number value to a valid double value and to infinity. The output of the program should be:
The number is not infinite.
The number is infinite.
Use isInfinite() in a custom program
You can use the isInfinite() method in your custom program to check the infinity of any double value. Here is an example of a program that uses the isInfinite() method to check a double value:
import java.util.Scanner;
public class CustomIsInfinite {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a double value: ");
double number = scanner.nextDouble();
if (Double.isInfinite(number)) {
System.out.println(number + " is infinite.");
} else {
System.out.println(number + " is not infinite.");
}
scanner.close();
}
}
Compile and run the code
Save the code in a file named CustomIsInfinite.java in the ~/project directory. Compile and run the code using the following commands:
javac ~/project/CustomIsInfinite.java && java CustomIsInfinite
Use isInfinite() with negative values
You can also use the isInfinite() method to check negative infinity. Here is an example of how to check negative infinity:
double negInfinity = Double.NEGATIVE_INFINITY;
if(Double.isInfinite(negInfinity)){
System.out.println("The number is negative infinite.");
} else {
System.out.println("The number is not negative infinite.");
}
Compile and run the code
Save the code in a file named NegativeInfinity.java in the ~/project directory. Compile and run the code using the following commands:
javac ~/project/NegativeInfinity.java && java NegativeInfinity
Summary
In this lab, you learned how to use the isInfinite() method of the Double class in Java. You also learned how to use this method to check if a double value is infinite or not. Now, you can easily use this method in your Java programs to check infinity values.



