int Integer之间使用 == 比较结果分析
前置知识:
java中使用 == 进行比较,基本数据类型(byte、short、int、long、float、double、char、boolean)比较的是值,引用类型比较的是地址。
使用equals()进行比较时,没有重写equals()方法,底层用的也是用 == 进行比较。
int Integer使用 == 比较分析
-
int 和 Integer的==比较时,integer自动拆箱成int,相当于基本类型的比较
-
Integer c = 10 和 Integer c1 = 10的==比较
首先int型的10自动装箱为Integer型,相当于调用了Integer.valueOf(10)的方法
自动装箱时首先判断i值是否在[-128,127]这个区间中,如果在这个区间中,则直接从IntegerCache.cache缓存中获取指定数字的包装类,
不在这个区间中则new出一个新的包装类
所以在[-128,127]之间返回true,超出则相当于创建出一个新的Integer对象,地址不同,则结果为falseIntegerCache内部实现了一个Integer的静态常量数组,在类加载的时候,执行static静态块进行初始化[-128,127]之间的Integer对象,
存放到cache数组中,cache数组属于常量,存放在java的方法区中,这样调用时会使用缓存池中的对象,多次调用会取得同一个对象的引用 -
Integer c = 10 和 Integer c1 = new Integer(10)的==比较
不同的对象,地址不同,结果当然不同 -
Integer c = new Integer(10) 和 Integer c1 = new Integer(10)的==比较
不同的对象,地址不同,结果当然不同
代码示例
public static void test(){
int a = 10;
int b = 1000;
Integer c = 10;
Integer c1 = 10;
Integer d = 1000;
Integer d1 = 1000;
Integer e = new Integer(10);
Integer e1 = new Integer(10);
Integer f = new Integer(1000);
Integer f1 = new Integer(1000);
/**
* int 和 Integer的==比较时,integer自动拆箱成int,相当于基本类型的比较
*/
System.out.println(a == c); // true
System.out.println(b == d); // true
System.out.println(a == e); // true
System.out.println(b == f); // true
/**
* Integer c = 10 和 Integer c1 = 10的==比较
* 首先int型的10自动装箱为Integer型,相当于调用了Integer.valueOf(10)的方法
* 自动装箱时首先判断i值是否在[-128,127]这个区间中,如果在这个区间中,则直接从IntegerCache.cache缓存中获取指定数字的包装类,
* 不在这个区间中则new出一个新的包装类
* 所以在[-128,127]之间返回true,超出则相当于创建出一个新的Integer对象,地址不同,则结果为false
*
* IntegerCache内部实现了一个Integer的静态常量数组,在类加载的时候,执行static静态块进行初始化[-128,127]之间的Integer对象,
* 存放到cache数组中,cache数组属于常量,存放在java的方法区中,这样调用时会使用缓存池中的对象,多次调用会取得同一个对象的引用
*/
System.out.println(c == c1); // true
System.out.println(d == d1); // false
/**
* Integer c = 10 和 Integer c1 = new Integer(10)的==比较
* 不同的对象,地址不同,结果当然不同
*/
System.out.println(c == e); // false
System.out.println(c1 == e1); // false
/**
* Integer c = new Integer(10) 和 Integer c1 = new Integer(10)的==比较
* 不同的对象,地址不同,结果当然不同
*/
System.out.println(e == e1); // false
System.out.println(f == f1); // false
}
Integer内部类IntegerCache源码
/**
* 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=<size>} 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.
*/
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[]; // [-128-127]常量缓存池
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() {}
}