Programming Tutorials

Understanding isInfinite() and isNaN() in Java

By: Emiley J in Java Tutorials on 2007-09-02  

Float and Double provide the methods isInfinite() and isNaN(), which help when manipulating two special double and float values. These methods test for two unique values defined by the IEEE floating-point specification: infinity and NaN (not a number).
isInfinite() returns true if the value being tested is infinitely large or small in magnitude. isNaN() returns true if the value being tested is not a number.

The following example creates two Double objects; one is infinite, and the other is not a number:

// Demonstrate isInfinite() and isNaN()
class InfNaN {
    public static void main(String args[]) {
        Double d1 = Double.valueOf(1 / 0.);
        Double d2 = Double.valueOf(0 / 0.);
        System.out.println(d1 + ": " + d1.isInfinite() + ", " +
        System.out.println(d2 + ": " + d2.isInfinite() + ", " +

This program generates the following output:

Infinity: true, false
NaN: false, true

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)