How to Use Infinity in Java

Written by amber d. walker
  • Share
  • Tweet
  • Share
  • Email

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.

Skill level:
Moderately Easy

Other People Are Reading


  1. 1

    Open your Java Integrated Development Environment (IDE).

  2. 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;

  3. 3

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





Tips and warnings

  • There is one other odd value defined in the Double and Float data types: NotaNumber (NAN). It is accessed with "Double.NaN" or "Float.NaN."
  • Infinity and NaN are returned by some trigonometric and logarithmic functions in the Java math package.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.