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

Most of the time you should use primitive number types in your code. For example:
int i = 500;
float gpa = 3.65;
byte mask = 0xff;
While the Java platform provides object classes that correspond to the primitive number classes, you may never need to use them. The following figure shows the hierarchy for the Number classes:

The class hierarchy of Number.

There are two reasons that you might use a Number class, instead of the primitive form: Also, BigInteger (in the API reference documentation) and BigDecimal (in the API reference documentation) complement the primitive data types 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.

The following table lists the instance methods that all the subclasses of the Number class implement.

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.
compareTo Compare this number object to the argument. Note that the Number class does not implement the Comparable interface, but most of the subtypes implement Comparable. So the Integer class has a compareTo(Integer) method, the BigDecimal class has a compareTo(BigDecimal) method, and so on.
boolean equals(Object) Determine whether this number object is equal to the argument. Note that the Number class does not override the equals method from Object. Most of the subtypes implement equals(Object), though they may not behave as you might expect. Consult the documentation for a particular Number subtype to see how the equals method behaves.

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.