站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

The Numbers Classes - Java Tutorial 5.0 英文版

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:
  • When an object is required — such as when using generic types. For example, say you want to create a list of ints. The generic ArrayList<E> class expects you to constrain your list to a particular type by replacing the E notation with a type argument, such as String. You cannot parameterize with a primitive type, so if you want a list of integers, you must specify the Integer type, as in ArrayList<Integer>. For more information, see Generics (in the Learning the Java Language trail).

  • When you need the variables or static methods defined by the class, such as MIN_VALUE and MAX_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.
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.