Java Double isInfinite Method

JavaJavaBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/classes_objects -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/class_methods -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/modifiers -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/oop -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/packages_api -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/user_input -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/wrapper_classes -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/identifier -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/arrays -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/data_types -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/if_else -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/operators -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/output -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/strings -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/variables -.-> lab-117611{{"`Java Double isInfinite Method`"}} java/system_methods -.-> lab-117611{{"`Java Double isInfinite Method`"}} end

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.

Other Java Tutorials you may like