390 Part II: The Java Library
Understanding isInfinite( ) and isNaN( )
FloatandDoubleprovide the methodsisInfinite( )andisNaN( ), which help when
manipulating two specialdoubleandfloatvalues. These methods test for two unique
values defined by the IEEE floating-point specification: infinity and NaN (not a number).
isInfinite( )returnstrueif the value being tested is infinitely large or small in magnitude.
isNaN( )returnstrueif the value being tested is not a number.
The following example creates twoDoubleobjects; one is infinite, and the other is not
a number:
// 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() + ", " + d1.isNaN());
System.out.println(d2 + ": " + d2.isInfinite() + ", " + d2.isNaN());
}
}
This program generates the following output:
Infinity: true, false
NaN: false, true
Byte, Short, Integer, and Long
TheByte,Short,Integer, andLongclasses are wrappers forbyte,short,int, andlonginteger
types, respectively. Their constructors are shown here:
Byte(bytenum)
Byte(Stringstr) throws NumberFormatException
Short(shortnum)
Short(Stringstr) throws NumberFormatException
Integer(intnum)
Integer(Stringstr) throws NumberFormatException
Long(longnum)
Long(Stringstr) throws NumberFormatException
As you can see, these objects can be constructed from numeric values or from strings that
contain valid whole number values.
The methods defined by these classes are shown in Tables 16-3 through 16-6. As you
can see, they define methods for parsing integers from strings and converting strings back
into integers. Variants of these methods allow you to specify theradix,or numeric base, for
conversion. Common radixes are 2 for binary, 8 for octal, 10 for decimal, and 16 for hexadecimal.