|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Most of the time you should use primitive number types in your code. For example: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 theint i = 500; float gpa = 3.65; byte mask = 0xff;Numberclasses:There are two reasons that you might use a Numberclass, instead of the primitive form:Also,
- When an object is required such as when using generic types. For example, say you want to create a list of
ints. The genericArrayList<E>class expects you to constrain your list to a particular type by replacing theEnotation with a type argument, such asString. You cannot parameterize with a primitive type, so if you want a list of integers, you must specify theIntegertype, as inArrayList<Integer>. For more information, see Generics.
- When you need the variables or static methods defined by the class, such as
MIN_VALUEandMAX_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.BigIntegerand
BigDecimalcomplement 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.langpackage,BigDecimalandBigIntegerare in thejava.mathpackage.The following table lists the instance methods that all the subclasses of the
Numberclass 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, anddouble.compareToCompare this number object to the argument. Note that the Numberclass does not implement theComparableinterface, but most of the subtypes implementComparable. So theIntegerclass has acompareTo(Integer)method, theBigDecimalclass has acompareTo(BigDecimal)method, and so on.boolean equals(Object)Determine whether this number object is equal to the argument. Note that the Numberclass does not override theequalsmethod fromObject. Most of the subtypes implementequals(Object), though they may not behave as you might expect. Consult the documentation for a particularNumbersubtype to see how theequalsmethod behaves.As a group, the
Numbersubclasses 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
FloatandDoubleclasses:
Other Useful Constants in the FloatandDoubleClassesMethod Description Float.NaN
Double.NaNNot a Number. Returned by certain methods in thejava.lang.Mathclass when the result is undefined for the arguments passed to the method.Float.NEGATIVE_INFINITY
Double.NEGATIVE_INFINITYThe negative infinity value for a floator 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.