Documentation

The Java™ Tutorials
Hide TOC
The Numbers Classes数字类
Trail: Learning the Java Language
Lesson: Numbers and Strings
Section: Numbers

The Numbers Classes数字类

When working with numbers, most of the time you use the primitive types in your code.在处理数字时,大多数情况下都会在代码中使用基本类型。For example:例如:

int i = 500;
float gpa = 3.65f;
byte mask = 0x7f;

There are, however, reasons to use objects in place of primitives, and the Java platform provides wrapper classes for each of the primitive data types.然而,使用对象代替原语是有原因的,Java平台为每种原语数据类型提供了包装类。These classes "wrap" the primitive in an object.这些类将原语“包装”到对象中。Often, the wrapping is done by the compiler—if you use a primitive where an object is expected, the compiler boxes the primitive in its wrapper class for you.通常,包装是由编译器完成的—如果在需要对象的位置使用原语,编译器会将原语装箱到其包装类中。Similarly, if you use a number object when a primitive is expected, the compiler unboxes the object for you.类似地,如果在需要原语时使用数字对象,编译器将为您拆箱该对象。For more information, see Autoboxing and Unboxing有关详细信息,请参阅自动装箱和拆箱

All of the numeric wrapper classes are subclasses of the abstract class Number:所有数字包装类都是抽象类Number的子类:

The class hierarchy of Number.

Note: There are four other subclasses of Number that are not discussed here.这里没有讨论Number的其他四个子类。BigDecimal and BigInteger are used for high-precision calculations.BigDecimalBigInteger用于高精度计算。AtomicInteger and AtomicLong are used for multi-threaded applications.AtomicIntegerAtomicLong用于多线程应用程序。

There are three reasons that you might use a Number object rather than a primitive:使用Number对象而不是原语有三个原因:

  1. As an argument of a method that expects an object (often used when manipulating collections of numbers).作为期望对象的方法的参数(通常在处理数字集合时使用)。
  2. To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.使用类定义的常量,例如提供数据类型上限和下限的MIN_VALUEMAX_VALUE
  3. To use class methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).使用类方法将值与其他基元类型进行转换,将值与字符串进行转换,并在数字系统(十进制、八进制、十六进制、二进制)之间进行转换。

The following table lists the instance methods that all the subclasses of the Number class implement.下表列出了Number类的所有子类实现的实例方法。

Methods Implemented by all Subclasses of NumberNumber的所有子类实现的方法
Method方法 Description描述
byte byteValue()
short shortValue()
int intValue()
long longValue()
float floatValue()
double doubleValue()
Converts the value of this Number object to the primitive data type returned.将此Number对象的值转换为返回的基本数据类型。
int compareTo(Byte anotherByte)
int compareTo(Double anotherDouble)
int compareTo(Float anotherFloat)
int compareTo(Integer anotherInteger)
int compareTo(Long anotherLong)
int compareTo(Short anotherShort)
Compares this Number object to the argument.将此Number对象与参数进行比较。
boolean equals(Object obj) Determines whether this number object is equal to the argument.确定此数字对象是否等于参数。
The methods return true if the argument is not null and is an object of the same type and with the same numeric value.如果参数不为null,并且是相同类型且具有相同数值的对象,则这些方法返回true
There are some extra requirements for Double and Float objects that are described in the Java API documentation.Java API文档中描述了对DoubleFloat对象的一些额外要求。

Each Number class contains other methods that are useful for converting numbers to and from strings and for converting between number systems.每个Number类都包含其他方法,这些方法对于在字符串之间转换数字以及在数字系统之间转换数字非常有用。The following table lists these methods in the Integer class.下表列出了Integer类中的这些方法。Methods for the other Number subclasses are similar:其他Number子类的方法类似:

Conversion Methods, Integer Class转换方法,Integer
Method方法 Description描述
static Integer decode(String s) Decodes a string into an integer.将字符串解码为整数。Can accept string representations of decimal, octal, or hexadecimal numbers as input.可以接受十进制、八进制或十六进制数字的字符串表示形式作为输入。
static int parseInt(String s) Returns an integer (decimal only).返回一个整数(仅限十进制)。
static int parseInt(String s, int radix) Returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input.返回整数,给定十进制、二进制、八进制或十六进制(radix分别等于10、2、8或16)数的字符串表示形式作为输入。
String toString() Returns a String object representing the value of this Integer.返回表示此Integer值的String对象。
static String toString(int i) Returns a String object representing the specified integer.返回表示指定整数的String对象。
static Integer valueOf(int i) Returns an Integer object holding the value of the specified primitive.返回包含指定基元值的Integer对象。
static Integer valueOf(String s) Returns an Integer object holding the value of the specified string representation.返回包含指定字符串表示形式的值的Integer对象。
static Integer valueOf(String s, int radix) Returns an Integer object holding the integer value of the specified string representation, parsed with the value of radix.返回一个Integer对象,该对象包含指定字符串表示形式的整数值,并用基数值进行分析。
For example, if s = "333" and radix = 8, the method returns the base-ten integer equivalent of the octal number 333.例如,如果s=“333”且基数=8,则该方法返回与八进制数333相等的以十为底的整数。

Previous page: Numbers
Next page: Formatting Numeric Print Output