java一共有9种包装器:Integer,Long,Float,Double,Short,Byte,Character,Void,Boolean,,这些对象包装器又有着与之对应的基本类型.对象包装器是不可变的,也是final的。我们什么时候会用到这些对象包装器呢?实际上,在声明一个数组列表时,由于其中的类型参数不能是基本类型,因此我们只能采用对象包装器。如下面的声明:
ArrayListlist=new ArrayList<>;
这里的Integer不能用int代替,这也是许多初学者容易犯的错误。但是我们在往上面的数组列表添加元素时,就可以直接添加int类型元素,如下面的语句:
list.add(5);
这里实际上编译器会进行自动装箱操作(autoboxing),即自动变为list.add(Integer.valueof(5)。相反的,当将一个Integer对象赋给一个int时,又会采用自动拆箱。并且,如果我们在一个条件表达式中混用Integer类型和Double类型,这里Integer就会自动拆箱,转变为int,然后再自动装箱,变为Double。
关于自动装箱和自动拆箱的实现,这里实际上是在编译器在生成字节码时就执行了,而非虚拟机执行的。我们在使用对象包装类时还可以调用一些它的基本方法,这也使得我们编写代码更加便捷:
Modifier and Type | Method | Description |
---|---|---|
static int |
bitCount(int i) |
Returns the number of one-bits in the two's complement binary representation of the specified
int value.
|
byte |
byteValue() |
Returns the value of this
Integer as a
byte after a narrowing primitive conversion.
|
static int |
compare(int x, int y) |
Compares two
int values numerically.
|
int |
compareTo(Integer anotherInteger) |
Compares two
Integer objects numerically.
|
static int |
compareUnsigned(int x, int y) |
Compares two
int values numerically treating the values as unsigned.
|
static Integer |
decode(String nm) |
Decodes a
String into an
Integer .
|
Optional |
describeConstable() |
Returns an
Optional containing the nominal descriptor for this instance, which is the instance itself.
|
static int |
divideUnsigned(int dividend, int divisor) |
Returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.
|
double |
doubleValue() |
Returns the value of this
Integer as a
double after a widening primitive conversion.
|
boolean |
equals(Object obj) |
Compares this object to the specified object.
|
float |
floatValue() |
Returns the value of this
Integer as a
float after a widening primitive conversion.
|
static Integer |
getInteger(String nm) |
Determines the integer value of the system property with the specified name.
|
static Integer |
getInteger(String nm, int val) |
Determines the integer value of the system property with the specified name.
|
static Integer |
getInteger(String nm, Integer val) |
Returns the integer value of the system property with the specified name.
|
int |
hashCode() |
Returns a hash code for this
Integer .
|
static int |
hashCode(int value) |
Returns a hash code for an
int value; compatible with
Integer.hashCode() .
|
static int |
highestOneBit(int i) |
Returns an
int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified
int value.
|
int |
intValue() |
Returns the value of this
Integer as an
int .
|
long |
longValue() |
Returns the value of this
Integer as a
long after a widening primitive conversion.
|
static int |
lowestOneBit(int i) |
Returns an
int value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified
int value.
|
static int |
max(int a, int b) |
Returns the greater of two
int values as if by calling
Math.max .
|
static int |
min(int a, int b) |
Returns the smaller of two
int values as if by calling
Math.min .
|
static int |
numberOfLeadingZeros(int i) |
Returns the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary representation of the specified
int value.
|
static int |
numberOfTrailingZeros(int i) |
Returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified
int value.
|
static int |
parseInt(CharSequence s, int beginIndex, int endIndex, int radix) |
Parses the
CharSequence argument as a signed
int in the specified
radix , beginning at the specified
beginIndex and extending to
endIndex - 1 .
|
static int |
parseInt(String s) |
Parses the string argument as a signed decimal integer.
|
static int |
parseInt(String s, int radix) |
Parses the string argument as a signed integer in the radix specified by the second argument.
|
static int |
parseUnsignedInt(CharSequence s, int beginIndex, int endIndex, int radix) |
Parses the
CharSequence argument as an unsigned
int in the specified
radix , beginning at the specified
beginIndex and extending to
endIndex - 1 .
|
static int |
parseUnsignedInt(String s) |
Parses the string argument as an unsigned decimal integer.
|
static int |
parseUnsignedInt(String s, int radix) |
Parses the string argument as an unsigned integer in the radix specified by the second argument.
|
static int |
remainderUnsigned(int dividend, int divisor) |
Returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.
|
Integer |
resolveConstantDesc(MethodHandles.Lookup lookup) |
Resolves this instance as a
ConstantDesc , the result of which is the instance itself.
|
static int |
reverse(int i) |
Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified
int value.
|
static int |
reverseBytes(int i) |
Returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified
int value.
|
static int |
rotateLeft(int i, int distance) |
Returns the value obtained by rotating the two's complement binary representation of the specified
int value left by the specified number of bits.
|
static int |
rotateRight(int i, int distance) |
Returns 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() |
Returns the value of this
Integer as a
short after a narrowing primitive conversion.
|
static int |
signum(int i) |
Returns the signum function of the specified
int value.
|
static int |
sum(int a, int b) |
Adds two integers together as per the + operator.
|
static String |
toBinaryString(int i) |
Returns a string representation of the integer argument as an unsigned integer in base 2.
|
static String |
toHexString(int i) |
Returns a string representation of the integer argument as an unsigned integer in base 16.
|
static String |
toOctalString(int i) |
Returns a string representation of the integer argument as an unsigned integer in base 8.
|
String |
toString() |
Returns a
String object representing this
Integer 's value.
|
static String |
toString(int i) |
Returns a
String object representing the specified integer.
|
static String |
toString(int i, int radix) |
Returns a string representation of the first argument in the radix specified by the second argument.
|
static long |
toUnsignedLong(int x) |
Converts the argument to a
long by an unsigned conversion.
|
static String |
toUnsignedString(int i) |
Returns a string representation of the argument as an unsigned decimal value.
|
static String |
toUnsignedString(int i, int radix) |
Returns a string representation of the first argument as an unsigned integer value in the radix specified by the second argument.
|
static Integer |
valueOf(int i) |
Returns an
Integer instance representing the specified
int value.
|
static Integer |
valueOf(String s) |
Returns an
Integer object holding the value of the specified
String .
|
static Integer |
valueOf(String s, int radix) |
Returns an
Integer object holding the value extracted from the specified
String when parsed with the radix given by the second argument.
|
以上就是Integer所有的方法说明和其返回值说明,其余包装类也有着类似的方法。