/**
* 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);
}
/**
* 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
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() {}
}
上面的代码片段是Java8中,Integer缓存池的源码,我们可以得出Integer缓存池大小默认为-128~127. 所以使用Integer x = 220进行赋值操作每次都会新建一个Integer对象,x == y也就会在控制台输出false了。如果我们像下面的代码片段那样定义,x == y就会输出true。
Integer x = 123;
Integer y = 123;
System.out.println(x ==y); // true
https://meisen.pro/article/d2e6e17a7daf4af39212e5a0de0645b2
近日在日常学习中看到一个让我很困惑的知识点:
这里控制台输出false. 这就让我十分的不解了,是什么机制导致了这结果?
要搞懂这个,先来讲讲Java的拆装箱。
装箱和拆箱
简单的说,装箱就是,自动将基本数据类型转换为包装器类型;拆箱就是,自动将包装器类型转换为基本数据类型。
基本类型都有对应的包装类型,基本类型与其对应的包装类型之间的赋值使用自动装箱与拆箱完成。
程序在执行到Integer x=123的时候会执行一次自动装箱(int->Integer),这一步的自动装箱实际上是调用了Integer.valueOf(int i)。
这里就出现了一个新的方法: Integer.valueOf(int i),那Integer.valueOf(int i)具体做了什么呢?
Integer.value(int i)
上面是Integer.valueOf(int i)的源码,可以从源码中得出该方法是先判断值是否在缓存池(IntegerCache)的缓存范围,如果在返回缓存池中的值,否则就是新创建一个Integer对象.
我们上面讲到通过字面量给Integer的包装类型引用赋值其实是调用了此方法,代码片段
x==y
控制台输出false, 我们可以valueOf的源码看出来,x
和y
的赋值其实每次都创建了一个Integer对象,而没有从缓存池中获取值。问题来了,怎么才会从缓存池中获取缓存值呢?Integer缓存池
上面的代码片段是Java8中,Integer缓存池的源码,我们可以得出Integer缓存池大小默认为
-128~127
. 所以使用Integer x = 220进行赋值操作每次都会新建一个Integer对象,x == y
也就会在控制台输出false了。如果我们像下面的代码片段那样定义,x == y
就会输出true。由于有缓存池,编译器会在自动装箱过程调用 valueOf()方法,因此多个Integer实例使用自动装箱来创建并且值相同,那么就会引用相同的对象。
扩展
new Integer(123)和Integer.valueOf(123)的区别