The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Object Basics and Simple Data Objects

The Numbers Classes

The following figure shows the class hierarchy for the number classes provided by the Java platform.

The class hierarchy of Number.

In addition to the number classes, the Java platform includes the Boolean (in the API reference documentation), Character (in the API reference documentation), and Void (in the API reference documentation) , which together with the number classes are known as the type-wrapper classes.

You might wonder why the type-wrapper classes are necessary, as they seem to duplicate the primitive data types. The type-wrapper classes have several uses.

Furthermore, BigInteger (in the API reference documentation) and BigDecimal (in the API reference documentation) extend the primitive data types in to allow for arbitrary-precision numbers (numbers that might not fit into any of the primitive data types). Note that whereas the other classes are in the java.lang package, BigDecimal and BigInteger are in the java.math package.

Here's an example, called NumberDemo (in a .java source file), that creates two Float objects and one Double object and then uses compareTo and equals to compare them:.

public class NumberDemo {
    public static void main(String args[]) {
        Float floatOne = new Float(14.78f - 13.78f);
        Float floatTwo = Float.valueOf("1.0");
        Double doubleOne = new Double(1.0);

        int difference = floatOne.compareTo(floatTwo);

	if (difference == 0) {
         System.out.println("floatOne is equal to floatTwo.");
        } else if (difference < 0) {
         System.out.println("floatOne is less than floatTwo.");
        } else if (difference > 0) {
         System.out.println("floatOne is greater than floatTwo.");
        }

        System.out.println("floatOne is "
                           + ((floatOne.equals(doubleOne)) ? 
                           "equal" : "not equal")
                           + " to doubleOne.");

    }
}
The output from this program might surprise you a little:
floatOne is equal to oneAgain.
floatOne is not equal to doubleOne.
Even though the values contained in floatOne and doubleOne are both numerically equal to 1, they are considered unequal because the objects are of different types.

The following table lists the instance methods that all the subclasses of the Number class contain, including the compareTo and equals methods used in the preceding example.

Instance Methods Common to the Number Classes
Method Description
byte byteValue()**
short shortValue()**
int intValue() long longValue()
float floatValue()
double doubleValue()
Convert the value of this number object to the primitive data types of byte, short, int, long, float, and double.
int compareTo(Integer)***
int compareTo(Object)***
Compare this number object to the argument. This method returns a number less than, equal to, or greater than 0, indicating that this number object is, respectively, less than, equal to, or greater than the argument.
boolean equals(Object) Determine whether this number object is equal to the argument.
** Added to the Number class and its subclasses for JDK 1.1.
*** Added to the Number subclasses for Java 2 SDK 1.2.

As a group, the Number subclasses also contain some useful constants. Because the constants are declared as public static, you refer to them by concatenating the class name, with a dot (.), with the constant name, as in: Integer.MIN_VALUE.

The following table lists other useful constants in the Float and Double classes:

Other Useful Constants in the Float and Double Classes
Method Description
Float.NaN
Double.NaN
Not a Number. Returned by certain methods in the java.lang.Math class when the result is undefined for the arguments passed to the method.
Float.NEGATIVE_INFINITY
Double.NEGATIVE_INFINITY
The negative infinity value for a float or a double.
Float.POSITIVE_INFINITY
Double.POSITIVE_INFINITY
The positive infinity value for a float or a double.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.