首先看一段代码:
public static void main(String[] args) {
Integer i1 = 100;
Integer i2 = 100;
System.out.println(i1 == i2);
Integer i3 = 200;
Integer i4 = 200;
System.out.println(i3 == i4);
}
执行结果为:

那如何避免这种情况呢?-- 很简单,遵循基本编码规范就行,即使用 equals() 方法来替换掉 "==" 运算符就行。

于是,以上代码便可这样写:

这样,便达到了我们的目的。
那么使用"=="去比较两个 Integer 值的时候,为什么会出现和预期不一样的效果呢?
执行代码段
Integer i1 = 100;
实际上执行的是
Integer i1 = Integer.valueOf(100);
那么,Integer.valueOf() 方法内部是怎么执行的呢?
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
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 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[];
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() {}
}
可以看到,在通过 valueOf() 方法创建 Integer 对象的时候,如果数值在 [-128,127] 之间,便返回指向 IntegerCache.cache 中已经存在的对象的引用;否则创建一个新的 Integer 对象。 i1 和 i2 的数值 100,因此会直接从 cache 中取已经存在的对象,所以 i1 和 i2 指向的是同一个对象,而 i3 和 i4 则是分别指向不同的对象。[-128,127] 这个范围便是 Integer包装类的默认缓存池,也可称为 Integer 常量池。
那么为什么使用 equals() 方法比较的时候能够得到预期的效果呢?简单来说就是 equals() 比较的是两个对象的值是否相等,而"=="则比较的是存放两个对象的地址是否相等。
equals() 与 == 的区别
equals() 与 == 最大的一个区别就是 equals() 是一个方法,而 == 是一个运算符。
== 如果比较对象是基本数据类型,则比较的是数值是否相等;如果比较的是引用数据类型,则比较的是对象的地址是否相等;
equals() 用来比较两个对象的内容是否相等。equals() 方法不能用于基本数据类型的变量,如果没有对 equals() 方法进行重写,则比较的是引用类型的变量所指向的对象的地址
966

被折叠的 条评论
为什么被折叠?



