Java Float isInfinite Method

JavaJavaBeginner
Practice Now

Introduction

The isInfinite() method is a built-in method of the Float class in Java. It is used to check whether a floating-point value is infinite or not. It returns true for an infinite value and false for a finite value.


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/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/booleans("`Booleans`") 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-117668{{"`Java Float isInfinite Method`"}} java/class_methods -.-> lab-117668{{"`Java Float isInfinite Method`"}} java/modifiers -.-> lab-117668{{"`Java Float isInfinite Method`"}} java/wrapper_classes -.-> lab-117668{{"`Java Float isInfinite Method`"}} java/identifier -.-> lab-117668{{"`Java Float isInfinite Method`"}} java/arrays -.-> lab-117668{{"`Java Float isInfinite Method`"}} java/booleans -.-> lab-117668{{"`Java Float isInfinite Method`"}} java/data_types -.-> lab-117668{{"`Java Float isInfinite Method`"}} java/if_else -.-> lab-117668{{"`Java Float isInfinite Method`"}} java/operators -.-> lab-117668{{"`Java Float isInfinite Method`"}} java/output -.-> lab-117668{{"`Java Float isInfinite Method`"}} java/strings -.-> lab-117668{{"`Java Float isInfinite Method`"}} java/variables -.-> lab-117668{{"`Java Float isInfinite Method`"}} java/system_methods -.-> lab-117668{{"`Java Float isInfinite Method`"}} end

Define the main method

The main() method is the entry point of the program. In this step, we'll define the main() method.

public static void main(String[] args) {

}

Create a float variable

In this step, we'll create a float variable and assign it a value. The value can be any finite or infinite float number. In this lab, we'll use Float.POSITIVE_INFINITY and Float.NaN values.

float myFloat = Float.POSITIVE_INFINITY;

Use isInfinite() method

In this step, we'll use the isInfinite() method to check whether the float value is infinite or not.

boolean infinity = Float.isInfinite(myFloat);

if(infinity == true){
  System.out.println("Value is infinite");
} else {
  System.out.println("Value is finite");
}

Here, Float.isInfinite(myFloat) will return true if myFloat value is infinite.

Use isNaN() method

In this step, we'll also use the isNaN() method to check whether the float value is not a number (NaN) or not.

boolean nan = Float.isNaN(myFloat);

if(nan == true){
  System.out.println("Value is not a number (NaN)");
} else {
  System.out.println("Value is not NaN");
}

Here, Float.isNaN(myFloat) will return true if myFloat value is NaN.

Save and Compile

Save the FloatingNumbers.java file and open your Terminal or Command Prompt. Compile the file using javac command:

javac FloatingNumbers.java

Run the program

Run the program using the java command:

java FloatingNumbers

You will see the output as:

Value is infinite
Value is not NaN

Summary

In this lab, we learned about the isInfinite() method of the Float class in Java, which is used to check whether a floating-point value is infinite or not. We also learned how to use the isNaN() method to check whether the float value is not a number or not.