先看下面这段代码的输出:
public class Main {
public static void main(String[] args){
Integer i1 = 127;
Integer i2 = 127;
Integer i3 = Integer.valueOf(127);
Integer i4 = new Integer(127);
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i1 == i2);//true
System.out.println(i1 == i3);//true
System.out.println(i1 == i4);//false
System.out.println(i5 == i6);//false
}
}
是不是与你预期的答案不一致,其实都是Cache的原因,别急,请带着问题耐心将这篇文章看下去。
parseInt方法
返回int基本数据类型,该方法有两个重载的方法。
parseInt(String s)
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}
parseInt(String s, int radix)
意思是求“int radix”进制数“String s”的10进制数是多少.比如parseInt(String s)中默认调用 parseInt(s,10)意思是:求10进制数“s”的10进制数是多少。
valueOf方法
用于将基本数据类型变成对象类型,俗称数据装箱。
该方法有三个重载方法,笔者重点介绍valueOf(int),因为其余两个重载方法都间接使用了该方法。
public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
}
装箱与拆箱机制:
装箱就是自动将基本数据类型转换为包装器类型,拆箱就是自动将包装器类型转换为基本数据类型。
用一段简单的代码片来说明Java自动拆箱与装箱的实现机制:
public class Main {
public static void main(String[] args){
Integer i = 66;
int n = i;
}
}
为了更加清楚的了解自动拆箱与装箱的过程,将上面的代码反编译后得到下图:
从反编译得到的字节码内容可以看出。在装箱的时候自动调用的是Integer的valueOf(int)方法,而在拆箱的时候自动调用的是Integer的intValue方法。
在文章开头提到,正是因为Cache的原因导致Integer比较时出现了一些另人费解的结果。下面将找出这个Cache。查阅了JDK1.8的源代码在Integer类的源码中发现有一个静态内部类IntegerCache。IntegerCache有一个静态的Integer数组,因为有静态代码块的存在,在类加载时就将-128 到 127 的Integer对象创建了,并填充在cache数组中,一旦程序调用valueOf 方法,如果i的值是在-128 到 127 之间就直接在cache缓存数组中去取Integer对象,从而形成对象服用。至于为什么这样设计:我想JDK设计者应该是想节省内存和提升性能。也许他们在很多的统计中发现这些数值范围内的值缓存命中率比较高。
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() {}
}
上面这段代码在JDK源码中注释如下:
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
大概的意思是说IntegerCache中cache数组的上界high是可以改变的:通过jvm参数[-XX:AutoBoxCacheMax=size]可以改变上界。
我们再次回到最初的问题:
public class Main {
public static void main(String[] args){
Integer i1 = 127;
Integer i2 = 127;
Integer i3 = Integer.valueOf(127);
Integer i4 = new Integer(127);
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i1 == i2);//true
System.out.println(i1 == i3);//true
System.out.println(i1 == i4);//false
System.out.println(i5 == i6);//false
}
}
(1)i1与i2发生了装箱的操作,装箱操作底层调用的valueOf()方法,valueOf()方法体里是有缓存操作的(上文有提)。当两个对象类型使用"=="比较时,比较的是变量的地址值。又因为 127 命中了IntegerCache缓存(-128~127)中之前创建好的Integer,所以i1与i2指向的是同一个内存。
(2)i1与i3与(1)是同样的道理,都是命中了IntegerCache缓存。
(3)new Integer(127)不会IntegerCache缓存的机制。Integer的源码如下:
public Integer(int value) {
this.value = value;
}
(4)128 超出了IntegerCache缓存的命中范围,于是重新创建了一个新的包装类型即在内存中开辟了一块新的空间。
除了Integer类,以下几种包装类也有cache机制:Byte、Short、Long、Character。需要注意的是,这四类的cache[]数组长度都是固定的,不可改变。且Byte、Short、Long的cache范围相同,[-128,127], Character则为[0,128]。Boolean取值只有2个,valueOf使用两个常量来自动装箱.
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));
}
}
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类的缓存机制:
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code true}.
*/
public static final Boolean TRUE = new Boolean(true);
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code false}.
*/
public static final Boolean FALSE = new Boolean(false);
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
注意Boolean取值只有2个,valueOf使用两个常量来自动装箱.