The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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 compilerif 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
的子类:
Number
that are not discussed here.Number
的其他四个子类。BigDecimal
and BigInteger
are used for high-precision calculations.BigDecimal
和BigInteger
用于高精度计算。AtomicInteger
and AtomicLong
are used for multi-threaded applications.AtomicInteger
和AtomicLong
用于多线程应用程序。
There are three reasons that you might use a 使用Number
object rather than a primitive:Number
对象而不是原语有三个原因:
MIN_VALUE
and MAX_VALUE
, that provide the upper and lower bounds of the data type.MIN_VALUE
和MAX_VALUE
。The following table lists the instance methods that all the subclasses of the 下表列出了Number
class implement.Number
类的所有子类实现的实例方法。
byte byteValue() |
Number object to the primitive data type returned.Number 对象的值转换为返回的基本数据类型。 |
int compareTo(Byte anotherByte) |
Number object to the argument.Number 对象与参数进行比较。 |
boolean equals(Object obj) |
true if the argument is not null and is an object of the same type and with the same numeric value.null ,并且是相同类型且具有相同数值的对象,则这些方法返回true 。Double and Float objects that are described in the Java API documentation.Double 和Float 对象的一些额外要求。 |
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
子类的方法类似:
static Integer decode(String s) |
|
static int parseInt(String s) |
|
static int parseInt(String s, int radix) |
radix equals 10, 2, 8, or 16 respectively) numbers as input.radix 分别等于10、2、8或16)数的字符串表示形式作为输入。 |
String toString() |
String object representing the value of this Integer .Integer 值的String 对象。 |
static String toString(int i) |
String object representing the specified integer.String 对象。 |
static Integer valueOf(int i) |
Integer object holding the value of the specified primitive.Integer 对象。 |
static Integer valueOf(String s) |
Integer object holding the value of the specified string representation.Integer 对象。 |
static Integer valueOf(String s, int radix) |
Integer object holding the integer value of the specified string representation, parsed with the value of radix.Integer 对象,该对象包含指定字符串表示形式的整数值,并用基数值进行分析。 |