How to Use Infinity in Java

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.

Though it is impossible for a computer to literally represent the value of infinity in memory, the Java "double" and "float" data-type reserves two slots in its address range that are understood by the computer to refer to positive and negative infinity.

Step 1

Open your Java Integrated Development Environment (IDE).

Advertisement

Video of the Day

Step 2

Type the following to define a double (or float) with a value of positive or negative infinity:

double pInfiniteDouble = Double.POSITIVE_INFINITY; double nInfiniteDouble = Double.NEGATIVE_INFINITY; float pInfiniteFloat = Float.POSITIVE_INFINITY; float nInfiniteFloat = Float.NEGATIVE_INFINITY;

Advertisement

Advertisement

Step 3

Check to see if a double or float has a value of infinity using the "isInfinite()" method:

pInfiniteDouble.isInfinite(); nInfiniteDouble.isInfinite(); pInfiniteFloat.isInfinite(); nInfiniteFloat.isInfinite();

Advertisement

Advertisement

Video of the Day

references

Report an Issue

screenshot of the current page

Screenshot loading...