Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The following figure shows the class hierarchy for the number classes provided by the Java platform.In addition to the number classes, the Java platform includes the Boolean
,Character
, andVoid
, 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,
- You can store a value of primitive type in a type-wrapper object whenever an object is required. For example, the
ArrayList
class can hold only objects, so if you want to put numbers in anArrayList
, you wrap each value in a type-wrapper object and provide that object to it. As you learned earlier in the discussion about characters, however, this conversion from primitive type to object happens automatically starting with JDK 5.0.
- The classes define useful variables, such as
MIN_VALUE
andMAX_VALUE
, that provide general information about the data type. The classes also define useful methods for converting values to other types, for converting to strings, and so on.
- The classes are used in the Java reflection mechanism, which allows programs to gather information about any object or class in the Java VM.
BigInteger
andBigDecimal
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 thejava.lang
package,BigDecimal
andBigInteger
are in thejava.math
package.Here's an example, called
NumberDemo
, that creates twoFloat
objects and oneDouble
object and then usescompareTo
andequals
to compare them:.The output from this program might surprise you a little: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."); } }Even though the values contained infloatOne is equal to oneAgain. floatOne is not equal to doubleOne.floatOne
anddoubleOne
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 thecompareTo
andequals
methods used in the preceding example.
** Added to the Number class and its subclasses for JDK 1.1.
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
, anddouble
.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 subclasses for Java 2 SDK 1.2.As a group, the
Number
subclasses also contain some useful constants. Because the constants are declared aspublic 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
andDouble
classes:
Other Useful Constants in the Float
andDouble
ClassesMethod Description Float.NaN
Double.NaNNot a Number
. Returned by certain methods in thejava.lang.Math
class when the result is undefined for the arguments passed to the method.Float.NEGATIVE_INFINITY
Double.NEGATIVE_INFINITYThe negative infinity value for a float
or adouble
.Float.POSITIVE_INFINITY
Double.POSITIVE_INFINITYThe positive infinity value for a float or a double.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.