Integer 是 int的封装类。有时候不得不用引用类型。比如集合里面的泛型。
如果在项目中你使用了Integer那么你可能就会陷入一个巨大的坑中。
Integer i=1290;
Integer ii=1290;
System.out.println(i==ii);//1 false
System.out.println(i.equals(ii));//2 true
Integer a=127;
Integer b=127;
System.out.println(a==b);//3 true
- 1为什么是fasle呢?这是因为‘’==‘’用在引用类型中判断的是地址。所以会是 false
- 3为什么等于true了呢?不理解怎么办?来一起看下源码。
先使用jd-jui看上面代码被虚拟机翻译成了是什么
Integer localInteger1 = Integer.valueOf(1290);
Integer localInteger2 = Integer.valueOf(1290);
System.out.println(localInteger1 == localInteger2);
System.out.println(localInteger1.equals(localInteger2));
Integer localInteger3 = Integer.valueOf(127);
Integer localInteger4 = Integer.valueOf(127);
System.out.println(localInteger3 == localInteger4);
通过反编译可以看出 Integer i=1290; 这样简写的方式 会被翻译成Integer.valueOf(1290)运行;
那就看下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);
}
大概意思就是说,为了提升性能这个方法会缓存-128到127的值。继续深入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;
}
…………
这是Integer的内部类,定义了Integer缓冲池最小值和最大值[-128,127],并且可以看出最大值可以根据本地环境自定义设置,默认127。
通过valueOf(int i) 这个方法我们就找到了答案。如果i>=-128&&i<=127就在缓冲池里取值,否则就是新创建一个对象。
- 再看一下 2 为什么为true,那我们就看下 Integer 的 equals方法的实现
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
相信大家都能看懂吧 哈哈