Understanding isInfinite( ) and isNaN( ) in Java

By: Emiley J  

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

// Demonstrate isInfinite() and isNaN()
class InfNaN {
public static void main(String args[]) {
Double d1 = new Double(1/0.);
Double d2 = new Double(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

Archived Comments

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial