一、构造函数
Integer(int value)
构造一个新分配的 Integer 对象,它表示指定的 int 值。
Integer(String s)
构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。
二、方法
int compareTo(Integer anotherInteger)
在数字上比较两个 Integer 对象。
boolean equals(Object obj)
比较此对象与指定对象。
double doubleValue()
以 double 类型返回该 Integer 的值。
float floatValue()
以 float 类型返回该 Integer 的值。
int intValue()
以 int 类型返回该 Integer 的值。
long longValue()
以 long 类型返回该 Integer 的值。
static int parseInt(String s)
将字符串参数作为有符号的十进制整数进行解析
static int parseInt(String s, int radix)
使用第二个参数指定的基数,将字符串参数解析为有符号的整数。
static String toBinaryString(int i)
以二进制(基数 2)无符号整数形式返回一个整数参数的字符串表示形式。
static String toHexString(int i)
以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式。
static String toOctalString(int i)
以八进制(基数 8)无符号整数形式返回一个整数参数的字符串表示形式。
String toString()
返回一个表示该 Integer 值的 String 对象。
static String toString(int i)
返回一个表示指定整数的 String 对象。
static String toString(int i, int radix)
返回用第二个参数指定基数表示的第一个参数的字符串表示形式。
static Integer valueOf(int i)
返回一个表示指定的 int 值的 Integer 实例。
static Integer valueOf(String s)
返回保存指定的 String 的值的 Integer 对象。
static Integer valueOf(String s, int radix)
返回一个 Integer 对象,该对象中保存了用第二个参数提供的基数进行解析时从指定的 String 中提取的值。
针对-128~127之间的数据,做了一个数据缓冲池,如果在该范围内,每次直接赋值给Integer并不会创建新的空间!
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
当我们给一个Integer对象赋一个int值的时候,会调用Integer类的静态方法valueOf
Integer a2 = 30; //自动装箱:Integer.valueOf(30)
Integer b2 = 30;
Integer c2 = Integer.valueOf(30);
Integer d2 = new Integer(30); //堆
System.out.println((a2 == b2)); //true
System.out.println((a2 == c2)); //true
System.out.println((a2 == d2)); //false
System.out.println(a2.equals(d2)); //true
由自动装箱和拆箱引发我看Integer源码
static int MAX_VALUE
值为 231-1 的常量,它表示 int 类型能够表示的最大值。
static int MIN_VALUE
值为 -231 的常量,它表示 int 类型能够表示的最小值。