大家讲道理2017-04-18 10:46:11
First, for both (first == second)
,因为 first 是 int(基本类型),所以在进行 == 比较的时候,second 会从 Integer
拆箱为 int
,然后 == 进行的是值比较 —— 因为两次值(第一次都为 127,第二次都为 128)都相等,所以两次都是 true
;
Looking at the source code of two (second == third)
,second 和 third 都是 Integer
(对象),所以 == 比较的时候,比较的是对象的引用,如果两个引用指向的是同一块内存(同一个对象),那么返回 true
,否则返回 false
。second = 127;
这样的代码其实是 Java 的语法糖,真正执行的是 second = Integer.valueOf(127)
,我们来看看 Integer.valueOf
:
It's easy to understand, Integer
的内部缓存了 IntegerCache.low ~ IntegerCache.high 的 Integer
,所以如果传入的整数参数 i 在这个范围之内的话,那么返回的就是缓存的对象,否则才新建一个 Integer
。在 Oracle 的 JVM 上,Integer
的缓存范围默认为 -128 ~ 127。所以每次调用 Integer.value(127)
返回的都是同一个被缓存的 Integer
,而调用 Integer.value(128)
将每次新建一个 Integer(new Integer(128))
。所以第一个(second == third)
returns true and the second one returns false.
怪我咯2017-04-18 10:46:11
Two Ineger types are compared with ==. If the value is between -128~127, it returns true, otherwise it returns false. This is due to the buffer object of Integer.valueof().
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() {}
}
伊谢尔伦2017-04-18 10:46:11
/**
* 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.
*/
Comparison between objects will affect the results due to buffering within a certain range.
Comparison between packaged class (object) and basic type is a direct ratio to get the result.