Java Double IsFinite Method

JavaJavaBeginner
Practice Now

Introduction

Java isFinite() method is used in Java language to check whether the passed double value is a finite value or not. It returns the boolean value true if the passed double value holds finite value, and returns false for NaN and infinite values. In this lab, you will learn how to use Java isFinite() method.


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/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-117609{{"`Java Double IsFinite Method`"}} java/classes_objects -.-> lab-117609{{"`Java Double IsFinite Method`"}} java/class_methods -.-> lab-117609{{"`Java Double IsFinite Method`"}} java/modifiers -.-> lab-117609{{"`Java Double IsFinite Method`"}} java/oop -.-> lab-117609{{"`Java Double IsFinite Method`"}} java/wrapper_classes -.-> lab-117609{{"`Java Double IsFinite Method`"}} java/identifier -.-> lab-117609{{"`Java Double IsFinite Method`"}} java/arrays -.-> lab-117609{{"`Java Double IsFinite Method`"}} java/data_types -.-> lab-117609{{"`Java Double IsFinite Method`"}} java/if_else -.-> lab-117609{{"`Java Double IsFinite Method`"}} java/operators -.-> lab-117609{{"`Java Double IsFinite Method`"}} java/output -.-> lab-117609{{"`Java Double IsFinite Method`"}} java/strings -.-> lab-117609{{"`Java Double IsFinite Method`"}} java/variables -.-> lab-117609{{"`Java Double IsFinite Method`"}} java/system_methods -.-> lab-117609{{"`Java Double IsFinite Method`"}} end

Set up the Java environment

If you have not installed Java in your system yet, download the latest version and install it. Verify the installation by running the following command:

java -version

Create a Java class

Create a new Java class called DoubleIsFiniteDemo in the ~/project directory.

cd ~/project
touch DoubleIsFiniteDemo.java

Write the Java code

In this step, we will write the Java code using isFinite() method to check whether the passed double value is a finite value or not. Copy and paste the following code into DoubleIsFiniteDemo.java:

public class DoubleIsFiniteDemo {
    public static void main(String[] args) {
        double d1 = 100.0;
        double d2 = Double.POSITIVE_INFINITY;
        double d3 = Double.NEGATIVE_INFINITY;
        double d4 = Double.NaN;

        boolean b1 = Double.isFinite(d1);
        boolean b2 = Double.isFinite(d2);
        boolean b3 = Double.isFinite(d3);
        boolean b4 = Double.isFinite(d4);

        System.out.println("Is d1 finite? " + b1);
        System.out.println("Is d2 finite? " + b2);
        System.out.println("Is d3 finite? " + b3);
        System.out.println("Is d4 finite? " + b4);
    }
}

The above code declares four double variables d1, d2, d3, and d4 with finite, positive infinity, negative infinity, and NaN values, respectively. Then, it uses isFinite() method to check whether each of the variables is finite or not.

Compile and run the Java code

Compile the code using the following command:

javac DoubleIsFiniteDemo.java

After compiling, you can run the code using the following command:

java DoubleIsFiniteDemo

The output of the program should be:

Is d1 finite? true
Is d2 finite? false
Is d3 finite? false
Is d4 finite? false

Customize the program

In this step, you can customize the program to test for different values. Open DoubleIsFiniteDemo.java in your favorite text editor and modify the values assigned to the double variables. For example:

public class DoubleIsFiniteDemo {
    public static void main(String[] args) {
        double d1 = 1.23;
        double d2 = 4.56;
        double d3 = Double.NEGATIVE_INFINITY;
        double d4 = Double.NaN;

        boolean b1 = Double.isFinite(d1);
        boolean b2 = Double.isFinite(d2);
        boolean b3 = Double.isFinite(d3);
        boolean b4 = Double.isFinite(d4);

        if (b1) {
            System.out.println("d1 is a finite double value.");
        } else {
            System.out.println("d1 is not a finite double value.");
        }

        if (b2) {
            System.out.println("d2 is a finite double value.");
        } else {
            System.out.println("d2 is not a finite double value.");
        }

        if (b3) {
            System.out.println("d3 is a finite double value.");
        } else {
            System.out.println("d3 is not a finite double value.");
        }

        if (b4) {
            System.out.println("d4 is a finite double value.");
        } else {
            System.out.println("d4 is not a finite double value.");
        }
    }
}

Recompile and run the Java code

Compile the code again using the following command:

javac DoubleIsFiniteDemo.java

After compiling, you can run the customized program using the following command:

java DoubleIsFiniteDemo

Depending on the values you entered, the program will output the result accordingly.

Use isFinite() method in an user-defined method

In this step, we will create a user-defined method called printIsFinite() that takes a double value as input and prints whether it is finite or not using isFinite() method.

Add the following method inside the DoubleIsFiniteDemo class:

public static void printIsFinite(double d) {
    if (Double.isFinite(d)) {
        System.out.println(d + " is a finite double value.");
    } else {
        System.out.println(d + " is not a finite double value.");
    }
}

Then, modify the main() method as follows:

public static void main(String[] args) {
    double d1 = 1.23;
    double d2 = 4.56;
    double d3 = Double.NEGATIVE_INFINITY;
    double d4 = Double.NaN;

    printIsFinite(d1);
    printIsFinite(d2);
    printIsFinite(d3);
    printIsFinite(d4);
}

The main method passes each of the four double variables to the printIsFinite() method, which then checks whether the value is finite or not and prints the result accordingly.

Recompile and run the Java code

Compile the code again using the following command:

javac DoubleIsFiniteDemo.java

After compiling, you can run the program using the following command:

java DoubleIsFiniteDemo

The output of the program should be:

1.23 is a finite double value.
4.56 is a finite double value.
-Infinity is not a finite double value.
NaN is not a finite double value.

Test the Java code with boundary values

In this step, we will test the Java code with boundary values to see how isFinite() method works.

Modify the main() method as follows:

public static void main(String[] args) {
    double d1 = Double.MIN_VALUE;
    double d2 = Double.MAX_VALUE;
    double d3 = Double.POSITIVE_INFINITY;
    double d4 = Double.NaN;

    printIsFinite(d1);
    printIsFinite(d2);
    printIsFinite(d3);
    printIsFinite(d4);
}

The main method uses the MIN_VALUE and MAX_VALUE constants of the Double class to assign the smallest and largest finite double values, respectively, to d1 and d2, and assigns positive infinity and NaN values to d3 and d4, respectively.

Recompile and run the Java code using the commands below:

javac DoubleIsFiniteDemo.java
java DoubleIsFiniteDemo

The output of the program should be:

4.9E-324 is a finite double value.
1.7976931348623157E308 is a finite double value.
Infinity is not a finite double value.
NaN is not a finite double value.

Summary

In this lab, you have learned how to use Java isFinite() method to check whether a double value is a finite value or not. You have also learned how to customize the program to test for different values, how to use isFinite() method in an user-defined method, and how to test the Java code with boundary values.