Java基本数据类型的包装类型大部分都用到了缓存,以此来提高性能。
Byte,Short,Integer,Long
这 4 种包装类默认创建了数值 [-128,127] 的相应类型的缓存数据,Character
创建了数值在 [0,127] 范围的缓存数据,Boolean
直接返回 True or False。
Byte 缓存源码:
private static class ByteCache {
private ByteCache(){}
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));
}
}
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
Short缓存源码:
private static class ShortCache {
private ShortCache(){}
static final Short cache[] = new Short[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Short((short)(i - 128));
}
}
public static Short valueOf(short s) {
final int offset = 128;
int sAsInt = s;
if (sAsInt >= -128 && sAsInt <= 127) { // must cache
return ShortCache.cache[sAsInt + offset];
}
return new Short(s);
}
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;
}
}
Long缓存源码:
public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}
private static class LongCache {
private LongCache(){}
static final Long cache[] = new Long[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Long(i - 128);
}
}
Character 缓存源码:
public static Character valueOf(char c) {
if (c <= 127) { // must cache
return CharacterCache.cache[(int)c];
}
return new Character(c);
}
private static class CharacterCache {
private CharacterCache(){}
static final Character cache[] = new Character[127 + 1];
static {
for (int i = 0; i < cache.length; i++)
cache[i] = new Character((char)i);
}
}
Boolean缓存源码:
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
如果超出对应范围仍然会去创建新的对象,缓存的范围区间的大小只是在性能和资源之间的权衡。
而两种浮点数类型的包装类 Float,Double
并没有实现缓存机制。
Integer i1 = 100;
Integer i2 = 100;
System.out.println(i1 == i2);// 输出 true
Float f1 = 66f;
Float f2 = 66f;
System.out.println(f1 == f2);// 输出 false
Double d1 = 9.9d;
Double d2 = 9.9d;
System.out.println(d1 == d2);// 输出 false
下面我们来看一下问题。下面的代码的输出结果是 true 还是 false 呢?
Integer i1 = 99;
Integer i2 = new Integer(99);
System.out.println(i1 == i2);
Integer i1=99
这一行代码会发生装箱,也就是说这行代码等价于 Integer i1=Integer.valueOf(99)
。因此,i1 直接使用的是缓存中的对象。而Integer i2 = new Integer(99)
会直接创建新的对象。
So,答案是 false 。