Integer类型在比较时需要注意以下几点:
- Integer的值在-128到127时,JVM在运行时创建了一个缓存区域,并创建了一个integer的数组,这个数组存储了-128至127的值。因此会复用已有缓存立的值,也就是说,这个区间的Integer可以直接用等号进行判断。
- Integer的值在-128到127之外时,Integer对象在堆上产生,不会复用已有缓存区的值,用等号会返回false。
- Integer数值在和int类型比较时会自动拆箱转换成int类型,只要数值相等,无论Integer类型数值是否超过范围,使用==比较都会返回true。
- Integer在使用new创建对象时,则两个对象即使值相等,在用==比较时也不相等,因为这两个值的对象地址不一样。
- 建议使用equals()方法来比较Integer对象。
- 当使用Integer i = 150初始化时,会调用Integer的静态方法Integer.valueOf(int i)
关于第一点,看了Integer对象的源码就会了解,这里我贴出关键代码以供参考:
/**
* 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() {}
}
Integer类中主要用到了一个静态内部类IntegerCache,就是在jvm加载的时候就会为Integer加载一个静态缓存,里边存储了-128-127的数值。
下面再看一下Integer的另一段代码:
/**
* 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);
}
/**
* The value of the {@code Integer}.
*
* @serial
*/
private final int value;
/**
* Constructs a newly allocated {@code Integer} object that
* represents the specified {@code int} value.
*
* @param value the value to be represented by the
* {@code Integer} object.
*/
public Integer(int value) {
this.value = value;
}
当使用Integer i = 150初始化时,会调用Integer的静态方法Integer.valueOf(int i),所以就想源码中所写的那样,当这个初始化数值在-128–127范围间它会直接取cache里的数组(实际存储在jvm的方法区(jdk1.8之前,1.8之后在堆内存中开辟了一块区域存放常量)),如果超过了范围,则会调用new()方法来重新申请一个内存空间(实际存储在jvm的堆内存中)
以上结论的测试代码如下:
Integer a=12;
Integer b=12;
Integer i1 = new Integer(12);
Integer i2 = new Integer(12);
System.out.println(a==b);//true
System.out.println(i1==i2);//false
Integer c=1000;
Integer d=1000;
int e = 1000;
System.out.println(c==d);//false
System.out.println(c.equals(d));//true
System.out.println(c==e);//true
以上内容如有不对之处,还请大家多多指教!感谢您的阅读:)