public final class Integer
extends Number implements Comparable<Integer>
| java.lang.Object | ||
| java.lang.Number | ||
| java.lang.Integer | ||
Integer类将原始类型int的值包装在对象中。 类型为Integer的对象包含类型为int的单个字段。
此外,本课提供了几种将 int转换为 String和 String转换为 int ,以及处理 int时有用的其他常量和方法。
实现注意事项:“比特 highestOneBit ”方法(如 highestOneBit和 numberOfTrailingZeros )的实现基于Henry numberOfTrailingZeros ,Jr.的 Hacker's Delight (Addison Wesley,2002)的材料。
常量(Constants) |
|
|---|---|
int |
BYTES 以二进制补码形式表示 |
int |
MAX_VALUE 一个常数保持最大值 |
int |
MIN_VALUE 一个常数保持最小值 |
int |
SIZE 以二进制补码形式表示 |
Fields |
|
|---|---|
public static final Class<Integer> |
TYPE 所述 |
Public constructors |
|
|---|---|
Integer(int value) 构造一个新分配的 |
|
Integer(String s) 构造一个新分配 |
|
公共方法(Public methods) |
|
|---|---|
static int |
bitCount(int i) 返回指定 |
byte |
byteValue() 返回此值 |
static int |
compare(int x, int y) 用数字比较两个 |
int |
compareTo(Integer anotherInteger) 以数字方式比较两个 |
static Integer |
decode(String nm) 将 |
double |
doubleValue() 返回此值 |
boolean |
equals(Object obj) 将此对象与指定的对象进行比较。 |
float |
floatValue() 返回此值 |
static Integer |
getInteger(String nm, Integer val) 返回具有指定名称的系统属性的整数值。 |
static Integer |
getInteger(String nm) 使用指定的名称确定系统属性的整数值。 |
static Integer |
getInteger(String nm, int val) 使用指定的名称确定系统属性的整数值。 |
int |
hashCode() 返回此 |
static int |
hashCode(int value) 返回 |
static int |
highestOneBit(int i) 在指定的 |
int |
intValue() 返回此值 |
long |
longValue() 返回此值 |
static int |
lowestOneBit(int i) 在指定的 |
static int |
max(int a, int b) |
static int |
min(int a, int b) |
static int |
numberOfLeadingZeros(int i) 返回指定的 |
static int |
numberOfTrailingZeros(int i) 返回指定的 |
static int |
parseInt(String s, int radix) 将字符串参数解析为第二个参数指定的基数中的带符号整数。 |
static int |
parseInt(String s) 将字符串参数解析为带符号的十进制整数。 |
static int |
reverse(int i) 返回通过颠倒指定的 |
static int |
reverseBytes(int i) 返回通过颠倒指定的 |
static int |
rotateLeft(int i, int distance) 返回将指定的 |
static int |
rotateRight(int i, int distance) 返回通过将指定的 |
short |
shortValue() 返回此值 |
static int |
signum(int i) 返回指定的 |
static int |
sum(int a, int b) 根据+运算符将两个整数相加。 |
static String |
toBinaryString(int i) 以基数2中的无符号整数形式返回整数参数的字符串表示形式。 |
static String |
toHexString(int i) 以16进制无符号整数形式返回整数参数的字符串表示形式。 |
static String |
toOctalString(int i) 以基数8中的无符号整数形式返回整数参数的字符串表示形式。 |
String |
toString() 返回代表此 |
static String |
toString(int i) 返回表示指定整数的 |
static String |
toString(int i, int radix) 返回由第二个参数指定的基数中第一个参数的字符串表示形式。 |
static Integer |
valueOf(String s) 返回保存指定的 |
static Integer |
valueOf(String s, int radix) 返回一个 |
static Integer |
valueOf(int i) 返回表示指定的 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Number
|
|
java.lang.Object
|
|
java.lang.Comparable
|
|
Integer (int value)
构造一个新分配的 Integer对象,该对象表示指定的 int值。
| 参数(Parameters) | |
|---|---|
value |
int: the value to be represented by the Integer object. |
Integer (String s)
构造一个新分配的Integer对象,该对象表示由String参数指示的int值。 该字符串将完全按照parseInt方法所使用的方式转换为int值。
| 参数(Parameters) | |
|---|---|
s |
String: the String to be converted to an Integer. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the String does not contain a parsable integer. |
int bitCount (int i)
返回指定int值的二进制补码二进制表示中的int 。 这个功能有时被称为人口数量 。
| 参数(Parameters) | |
|---|---|
i |
int
|
| 返回(Returns) | |
|---|---|
int |
the number of one-bits in the two's complement binary representation of the specified int value. |
byte byteValue ()
返回此值 Integer为 byte 。
| 返回(Returns) | |
|---|---|
byte |
the numeric value represented by this object after conversion to type byte. |
int compare (int x,
int y)
以数字方式比较两个int值。 返回的值与以下内容返回的值相同:
Integer.valueOf(x).compareTo(Integer.valueOf(y))
| 参数(Parameters) | |
|---|---|
x |
int: the first int to compare |
y |
int: the second int to compare |
| 返回(Returns) | |
|---|---|
int |
the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y |
int compareTo (Integer anotherInteger)
以数字方式比较两个 Integer对象。
| 参数(Parameters) | |
|---|---|
anotherInteger |
Integer: the Integer to be compared. |
| 返回(Returns) | |
|---|---|
int |
the value 0 if this Integer is equal to the argument Integer; a value less than 0 if this Integer is numerically less than the argument Integer; and a value greater than 0 if this Integer is numerically greater than the argument Integer (signed comparison). |
Integer decode (String nm)
将String解码为Integer 。 接受由以下语法给出的十进制,十六进制和八进制数字:
DecimalNumeral, HexDigits, and OctalDigits are as defined in section 3.10.1 of The JavaTM Language Specification, except that underscores are not accepted between digits.
- DecodableString:
- Signopt DecimalNumeral
- Signopt
0xHexDigits- Signopt
0XHexDigits- Signopt
#HexDigits- Signopt
0OctalDigits- Sign:
-+
可选符号和/或基数说明符(“ 0x ”,“ 0X ”,“ # ”或前导零)后面的字符序列将通过Integer.parseInt方法用指示的基数(10,16或8)进行解析。 这个字符序列必须表示一个正值, NumberFormatException将抛出一个NumberFormatException 。 如果指定的String第一个字符是负号,则结果将被否定。 String中不允许有空格字符。
| 参数(Parameters) | |
|---|---|
nm |
String: the String to decode. |
| 返回(Returns) | |
|---|---|
Integer |
an Integer object holding the int value represented by nm |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the String does not contain a parsable integer. |
double doubleValue ()
返回此值 Integer为 double 。
| 返回(Returns) | |
|---|---|
double |
the numeric value represented by this object after conversion to type double. |
boolean equals (Object obj)
将此对象与指定的对象进行比较。 当且仅当参数不是null并且是包含与此对象相同的int值的Integer对象时,结果为true 。
| 参数(Parameters) | |
|---|---|
obj |
Object: the object to compare with. |
| 返回(Returns) | |
|---|---|
boolean |
true if the objects are the same; false otherwise. |
float floatValue ()
返回此值 Integer为 float 。
| 返回(Returns) | |
|---|---|
float |
the numeric value represented by this object after conversion to type float. |
Integer getInteger (String nm, Integer val)
返回具有指定名称的系统属性的整数值。 第一个参数被视为系统属性的名称。 系统属性可通过getProperty(java.lang.String)方法访问。 然后按照Integer.decode方法将该属性的字符串值解释为整数值,并返回代表该值的Integer对象。
0x or the ASCII character #, not followed by a minus sign, then the rest of it is parsed as a hexadecimal integer exactly as by the method valueOf(java.lang.String, int) with radix 16. 0 followed by another character, it is parsed as an octal integer exactly as by the method valueOf(java.lang.String, int) with radix 8. valueOf(java.lang.String, int) with radix 10. 第二个参数是默认值。 如果没有指定名称的属性,属性没有正确的数字格式,或者指定的名称为空或null ,则返回默认值。
| 参数(Parameters) | |
|---|---|
nm |
String: property name. |
val |
Integer: default value. |
| 返回(Returns) | |
|---|---|
Integer |
the Integer value of the property. |
Integer getInteger (String nm)
使用指定的名称确定系统属性的整数值。
第一个参数被视为系统属性的名称。 系统属性可通过getProperty(java.lang.String)方法访问。 然后将该属性的字符串值解释为一个整数值,并返回表示此值的对象Integer 。 可能的数字格式的细节可以在getProperty的定义中getProperty 。
如果没有指定名称的属性,如果指定的名称为空或 null ,或者属性的数字格式不正确,则返回 null 。
换句话说,该方法返回一个 Integer对象,其值等于:
getInteger(nm, null)
| 参数(Parameters) | |
|---|---|
nm |
String: property name. |
| 返回(Returns) | |
|---|---|
Integer |
the Integer value of the property. |
Integer getInteger (String nm, int val)
使用指定的名称确定系统属性的整数值。
第一个参数被视为系统属性的名称。 系统属性可通过getProperty(java.lang.String)方法访问。 然后将该属性的字符串值解释为整数值,并返回表示此值的Integer对象。 可能的数字格式的细节可以在getProperty的定义中getProperty 。
第二个参数是默认值。 如果没有指定名称的属性,如果该属性不具有正确的数字格式,或者指定的名称为空或null ,则返回表示第二个参数值的Integer对象。
换句话说,这个方法返回一个 Integer对象,其值等于:
getInteger(nm, new Integer(val))
but in practice it may be implemented in a manner such as:
to avoid the unnecessary allocation of anInteger result = getInteger(nm, null); return (result == null) ? new Integer(val) : result;
Integer object when the default value is not needed.
| 参数(Parameters) | |
|---|---|
nm |
String: property name. |
val |
int: default value. |
| 返回(Returns) | |
|---|---|
Integer |
the Integer value of the property. |
int hashCode ()
返回此 Integer的哈希码。
| 返回(Returns) | |
|---|---|
int |
a hash code value for this object, equal to the primitive int value represented by this Integer object. |
int hashCode (int value)
返回int值的哈希码; 与Integer.hashCode()兼容。
| 参数(Parameters) | |
|---|---|
value |
int: the value to hash |
| 返回(Returns) | |
|---|---|
int |
a hash code value for a int value. |
int highestOneBit (int i)
在指定的int值的最高位(“最左边”)一位中返回一个int值,最多只有一位。 如果指定的值在其二进制补码表示中没有一位,即等于零,则返回零。
| 参数(Parameters) | |
|---|---|
i |
int
|
| 返回(Returns) | |
|---|---|
int |
an int value with a single one-bit, in the position of the highest-order one-bit in the specified value, or zero if the specified value is itself equal to zero. |
int intValue ()
返回此值 Integer为 int 。
| 返回(Returns) | |
|---|---|
int |
the numeric value represented by this object after conversion to type int. |
long longValue ()
返回此值 Integer为 long 。
| 返回(Returns) | |
|---|---|
long |
the numeric value represented by this object after conversion to type long. |
int lowestOneBit (int i)
在指定的int值的最低位(“最右边”)一位中返回一个int值,最多只有一位。 如果指定的值在其二进制补码表示中没有一位,即等于零,则返回零。
| 参数(Parameters) | |
|---|---|
i |
int
|
| 返回(Returns) | |
|---|---|
int |
an int value with a single one-bit, in the position of the lowest-order one-bit in the specified value, or zero if the specified value is itself equal to zero. |
int max (int a,
int b)
通过调用 Math.max返回两个 int值中较大的 Math.max 。
| 参数(Parameters) | |
|---|---|
a |
int: the first operand |
b |
int: the second operand |
| 返回(Returns) | |
|---|---|
int |
the greater of a and b |
也可以看看:
int min (int a,
int b)
通过调用 Math.min返回两个 int值中较小的 Math.min 。
| 参数(Parameters) | |
|---|---|
a |
int: the first operand |
b |
int: the second operand |
| 返回(Returns) | |
|---|---|
int |
the smaller of a and b |
也可以看看:
int numberOfLeadingZeros (int i)
返回指定int值的二进制补码表示中最高位(“最左边”)一位之前的零位数。 如果指定值的二进制补码表示中没有一位,换句话说,如果它等于零,则返回32。
请注意,此方法与对数基数2密切相关。对于所有正的 int值x:
31 - numberOfLeadingZeros(x) 32 - numberOfLeadingZeros(x - 1) | 参数(Parameters) | |
|---|---|
i |
int
|
| 返回(Returns) | |
|---|---|
int |
the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary representation of the specified int value, or 32 if the value is equal to zero. |
int numberOfTrailingZeros (int i)
返回指定的int值的二进制补码二进制表示中的最低位(“最右边”)一位后的零位数。 如果指定值的二进制补码表示中没有一位,换句话说,如果它等于零,则返回32。
| 参数(Parameters) | |
|---|---|
i |
int
|
| 返回(Returns) | |
|---|---|
int |
the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified int value, or 32 if the value is equal to zero. |
int parseInt (String s, int radix)
将字符串参数解析为第二个参数指定的基数中的带符号整数。 字符串中的字符必须全部是指定基数的数字(由digit(char, int)是否返回非负值决定),但第一个字符可能是ASCII减号'-' ( '\u002D' )以指示负值或ASCII加号'+' ( '\u002B' )表示正值。 返回结果整数值。
如果发生以下任何情况,则会引发类型 NumberFormatException的异常:
null or is a string of length zero. MIN_RADIX or larger than MAX_RADIX. '-' ('\u002D') or plus sign '+' ('\u002B') provided that the string is longer than length 1. int. 例子:
parseInt("0", 10) returns 0
parseInt("473", 10) returns 473
parseInt("+42", 10) returns 42
parseInt("-0", 10) returns 0
parseInt("-FF", 16) returns -255
parseInt("1100110", 2) returns 102
parseInt("2147483647", 10) returns 2147483647
parseInt("-2147483648", 10) returns -2147483648
parseInt("2147483648", 10) throws a NumberFormatException
parseInt("99", 8) throws a NumberFormatException
parseInt("Kona", 10) throws a NumberFormatException
parseInt("Kona", 27) returns 411787
| 参数(Parameters) | |
|---|---|
s |
String: the String containing the integer representation to be parsed |
radix |
int: the radix to be used while parsing s. |
| 返回(Returns) | |
|---|---|
int |
the integer represented by the string argument in the specified radix. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the String does not contain a parsable int. |
int parseInt (String s)
将字符串参数解析为带符号的十进制整数。 字符串中的字符必须全部为十进制数字,但第一个字符可能是ASCII减号'-' ( '\u002D' )以指示负值或ASCII加号'+' ( '\u002B' )表示正值。 返回结果整数值,就好像参数和基数10作为parseInt(java.lang.String, int)方法的参数parseInt(java.lang.String, int) 。
| 参数(Parameters) | |
|---|---|
s |
String: a String containing the int representation to be parsed |
| 返回(Returns) | |
|---|---|
int |
the integer value represented by the argument in decimal. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the string does not contain a parsable integer. |
int reverse (int i)
返回通过颠倒指定 int值的二进制补码二进制表示中位的顺序而获得的值。
| 参数(Parameters) | |
|---|---|
i |
int
|
| 返回(Returns) | |
|---|---|
int |
the value obtained by reversing order of the bits in the specified int value. |
int reverseBytes (int i)
返回通过颠倒指定的 int值的二进制补码表示中的字节顺序而获得的值。
| 参数(Parameters) | |
|---|---|
i |
int
|
| 返回(Returns) | |
|---|---|
int |
the value obtained by reversing the bytes in the specified int value. |
int rotateLeft (int i,
int distance)
返回通过将指定的int值的二进制补码表示旋转指定的int值。 (位偏左或高位侧重新进入右侧或低位。)
请注意,具有负距离的左旋转等效于右旋: rotateLeft(val, -distance) == rotateRight(val, distance) 。 还要注意的是,32的任意倍数的旋转是无操作的,因此即使距离为负数,旋转距离的所有位数都可以忽略: rotateLeft(val, distance) == rotateLeft(val, distance & 0x1F) 。
| 参数(Parameters) | |
|---|---|
i |
int
|
distance |
int
|
| 返回(Returns) | |
|---|---|
int |
the value obtained by rotating the two's complement binary representation of the specified int value left by the specified number of bits. |
int rotateRight (int i,
int distance)
返回通过将指定的int值的二进制补码表示向右旋转指定的int值。 (位右移位,或者低位侧,再次进入左边或者高位。)
请注意,具有负距离的右旋转相当于左旋: rotateRight(val, -distance) == rotateLeft(val, distance) 。 还要注意的是,以32的任意倍数旋转是无操作的,因此即使距离为负数,旋转距离的最后五位也可以忽略: rotateRight(val, distance) == rotateRight(val, distance & 0x1F) 。
| 参数(Parameters) | |
|---|---|
i |
int
|
distance |
int
|
| 返回(Returns) | |
|---|---|
int |
the value obtained by rotating the two's complement binary representation of the specified int value right by the specified number of bits. |
short shortValue ()
返回此值 Integer为 short 。
| 返回(Returns) | |
|---|---|
short |
the numeric value represented by this object after conversion to type short. |
int signum (int i)
返回指定的int值的符号函数。 (如果指定值为负,则返回值为-1;如果指定值为零,则返回0;如果指定值为正数,则返回1。)
| 参数(Parameters) | |
|---|---|
i |
int
|
| 返回(Returns) | |
|---|---|
int |
the signum function of the specified int value. |
int sum (int a,
int b)
根据+运算符将两个整数相加。
| 参数(Parameters) | |
|---|---|
a |
int: the first operand |
b |
int: the second operand |
| 返回(Returns) | |
|---|---|
int |
the sum of a and b |
也可以看看:
String toBinaryString (int i)
以基数2中的无符号整数形式返回整数参数的字符串表示形式。
如果参数是负数,则无符号整数值是参数加2 32 ; 否则它等于论证。 该值在二进制(基2)中转换为一串ASCII数字,没有额外的前导0 。 如果无符号大小为零,则它由单个零字符'0' ( '\u0030' )表示; 否则,无符号大小的表示的第一个字符将不是零字符。 字符'0' ( '\u0030' )和'1' ( '\u0031' )用作二进制数字。
| 参数(Parameters) | |
|---|---|
i |
int: an integer to be converted to a string. |
| 返回(Returns) | |
|---|---|
String |
the string representation of the unsigned integer value represented by the argument in binary (base 2). |
String toHexString (int i)
以16进制无符号整数形式返回整数参数的字符串表示形式。
如果参数是负数,则无符号整数值是参数加2 32 ; 否则,它等于论证。 该值被转换为十六进制的ASCII数字字符串(基数16),没有额外的前导0 。 如果无符号大小为零,则它由单个零字符'0' ( '\u0030' )表示; 否则,无符号大小的表示的第一个字符将不是零字符。 以下字符用作十六进制数字:
0123456789abcdef
These are the characters
'\u0030' through
'\u0039' and
'\u0061' through
'\u0066'. If uppercase letters are desired, the
toUpperCase() method may be called on the result:
Integer.toHexString(n).toUpperCase()
| 参数(Parameters) | |
|---|---|
i |
int: an integer to be converted to a string. |
| 返回(Returns) | |
|---|---|
String |
the string representation of the unsigned integer value represented by the argument in hexadecimal (base 16). |
String toOctalString (int i)
以基数8中的无符号整数形式返回整数参数的字符串表示形式。
如果参数是负数,则无符号整数值是参数加2 32 ; 否则,它等于论证。 该值被转换为八进制(基数8)的ASCII数字串,没有额外的前导0 。
如果无符号大小为零,则它由单个零字符'0' ( '\u0030' )表示; 否则,无符号大小的表示的第一个字符将不是零字符。 以下字符用作八进制数字:
01234567
These are the characters
'\u0030' through
'\u0037'.
| 参数(Parameters) | |
|---|---|
i |
int: an integer to be converted to a string. |
| 返回(Returns) | |
|---|---|
String |
the string representation of the unsigned integer value represented by the argument in octal (base 8). |
String toString ()
返回代表此Integer的值的String对象。 该值被转换为带符号的十进制表示并作为字符串返回,就好像整数值作为toString(int)方法的参数toString(int) 。
| 返回(Returns) | |
|---|---|
String |
a string representation of the value of this object in base 10. |
String toString (int i)
返回表示指定整数的String对象。 该参数被转换为带符号的十进制表示并作为字符串返回,就好像参数和基数10作为toString(int, int)方法的参数toString(int, int) 。
| 参数(Parameters) | |
|---|---|
i |
int: an integer to be converted. |
| 返回(Returns) | |
|---|---|
String |
a string representation of the argument in base 10. |
String toString (int i, int radix)
返回由第二个参数指定的基数中第一个参数的字符串表示形式。
如果基数小于 Character.MIN_RADIX或大于 Character.MAX_RADIX ,则使用基数 10代替。
如果第一个参数是负数,结果的第一个元素是ASCII减号字符'-' ( '\u002D' )。 如果第一个参数不是负数,则结果中不会出现符号字符。
结果的其余字符表示第一个参数的大小。 如果幅度为零,则用单个零字符'0' ( '\u0030' )表示; 否则,幅度表示的第一个字符将不是零字符。 以下ASCII字符用作数字:
0123456789abcdefghijklmnopqrstuvwxyz
These are
'\u0030' through
'\u0039' and
'\u0061' through
'\u007A'. If
radix is
N, then the first
N of these characters are used as radix-
N digits in the order shown. Thus, the digits for hexadecimal (radix 16) are
0123456789abcdef. If uppercase letters are desired, the
toUpperCase() method may be called on the result:
Integer.toString(n, 16).toUpperCase()
| 参数(Parameters) | |
|---|---|
i |
int: an integer to be converted to a string. |
radix |
int: the radix to use in the string representation. |
| 返回(Returns) | |
|---|---|
String |
a string representation of the argument in the specified radix. |
Integer valueOf (String s)
返回保存指定String的值的Integer对象。 该参数被解释为表示一个带符号的十进制整数,就像参数被赋予parseInt(java.lang.String)方法一样。 结果是表示由字符串指定的整数值的Integer对象。
换句话说,该方法返回一个 Integer对象,其值等于:
new Integer(Integer.parseInt(s))
| 参数(Parameters) | |
|---|---|
s |
String: the string to be parsed. |
| 返回(Returns) | |
|---|---|
Integer |
an Integer object holding the value represented by the string argument. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the string cannot be parsed as an integer. |
Integer valueOf (String s, int radix)
返回一个Integer对象,该对象保存从第二个参数给出的基数进行分析时从指定的String提取的值。 第一个参数被解释为表示由第二个参数指定的基数中的有符号整数,就像参数被赋予parseInt(java.lang.String, int)方法一样。 结果是表示由字符串指定的整数值的Integer对象。
换句话说,该方法返回一个 Integer对象,其值等于:
new Integer(Integer.parseInt(s, radix))
| 参数(Parameters) | |
|---|---|
s |
String: the string to be parsed. |
radix |
int: the radix to be used in interpreting s |
| 返回(Returns) | |
|---|---|
Integer |
an Integer object holding the value represented by the string argument in the specified radix. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the String does not contain a parsable int. |
Integer valueOf (int i)
返回表示指定的int值的Integer实例。 如果不需要新的Integer实例,通常应优先使用此方法,而不是构造函数Integer(int) ,因为此方法通过缓存频繁请求的值可能会产生显着更好的空间和时间性能。 此方法将始终缓存-128至127(含)范围内的值,并可缓存此范围之外的其他值。
| 参数(Parameters) | |
|---|---|
i |
int: an int value. |
| 返回(Returns) | |
|---|---|
Integer |
an Integer instance representing i. |