int的包装类就是Integer,从Java 5开始引入了自动装箱/拆箱机制,使得二者可以相互转换。
Integer.valueOf(int)
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
这里 IntegerCache.cache
是Integer 中的静态缓存内部类,系统默认只缓存 -128~127 之间的整数。
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() {}
}
在创建数字 1 的对象时, 大多数人会使用 new Integer(1),
而使用 Integer.valueOf(1) 可以使用系统缓存,既减少可能的内存占用,也省去了频繁创建对象的开销。
小牛试刀
- 一
public static void main(String[] args) {
Integer a = new Integer(3);
Integer b = 3;
int c = 3;
System.out.println(a == b);
System.out.println(a == c);
}
不经意看好像都是 true 。
解答:
int c = 3;
Integer a = new Integer(3);
// a 这个毋庸置疑是new 了一个新内存。
Integer b = 3;
// 127 > b > -128 走的IntegerCache,取得是缓存里的对象。
// 所以 : a == b // false
// a == c
System.out.println(a==b); // false
System.out.println(a==c); // true a自动拆箱成int类型再和c比较
System.out.println(b==c); // true
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
- 二
public static void main(String[] args) {
Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
System.out.println(f1 == f2);
System.out.println(f3 == f4);
}
看了第一题再看这道题,应该有点头绪了。
先需要注意的是f1、f2、f3、f4四个变量都是Integer对象引用,所以==运算比较的不是值而是引用
。装箱的本质是什么呢?当我们给一个Integer对象赋一个int值的时候,会调用Integer类的静态方法valueOf()
解答:
显然 f1 、f2 走的IntegerCache , 得到的是同一个Integer 对象 。 f1 == f2 // true
f3 、f4 >127,并没缓存。获取到的是两个不同Integer 对象。 f3 == f4 // false