自动装箱
基本类型转换为包装类型
由此可以判断 如果是使用new那么每次都会返回false。因此使用的是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);
}
使用数组缓存,如果要转换的数在-128到127之间,直接返回对象,否则创建一个新的对象。
这样可以提升效率。
自动拆箱
包装类型转换为基本类型
底层隐式调用了intValue()方法。