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;Number
classes:There are two reasons that you might use a Number
class, 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
int
s. The genericArrayList<E>
class expects you to constrain your list to a particular type by replacing theE
notation with a type argument, such asString
. You cannot parameterize with a primitive type, so if you want a list of integers, you must specify theInteger
type, as inArrayList<Integer>
. For more information, see Generics.
- When you need the variables or static methods defined by the class, 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.BigInteger
andBigDecimal
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 thejava.lang
package,BigDecimal
andBigInteger
are in thejava.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
, anddouble
.compareTo
Compare this number object to the argument. Note that the Number
class does not implement theComparable
interface, but most of the subtypes implementComparable
. So theInteger
class has acompareTo(Integer)
method, theBigDecimal
class has acompareTo(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 theequals
method fromObject
. Most of the subtypes implementequals(Object)
, though they may not behave as you might expect. Consult the documentation for a particularNumber
subtype to see how theequals
method behaves.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.