java中有8种基本数据类型,破坏了java为纯面向对象的特征。为了承诺在java中一切皆对象,java又给每种基本数据类型分别匹配了一个类,这个类我们称之为包装类/封装类。本文以Integer为例,简单讲解其底层原理
我们都知道包装类和基本数据类型之间会进行自动装箱和自动拆箱,如Integer integer =10;//自动装箱,其本质是Integer integer =Integer.valueOf(10);//自动装箱
Integer.valueOf(10)底层原理
以下是主要的源码,注意我的中文注释内容
/**
//这部分是Integer的缓冲区类,也就是说,根据JLS(Java语言规范)缓冲区内有-128到127的整数的数组,而且是在第一次使用时就自动加载的,这个整数数组在常量池中,注意在常量池中,很重要。尽管Java语言规范建议缓冲区数组是-128到127的整数,但是我们可以在VM中设置最大值,也就是说127是可以变的,根据-XX:AutoBoxCacheMax=<size>可以设置。但是最大值不能超过Integer.MAX_VALUE - (-low) -1。
* 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;//最小值-128
static final int high;//最大值
static final Integer cache[];//缓冲区数组
static {
// high value may be configured by property
int h = 127;//默认最大值为127
//获取VM设置的最大值
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);//取设置的最大值和默认的127间的最大值
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);//最大值不能超过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类的代码,也是自动装箱的代码,如果传入的int整数在缓冲区内(注意这个缓冲区是-128到127或者-128到你在VM中设置的值)就直接取出,也就是冲常量池中取出,否则就新建一个对象,注意这里是重点,不在缓冲区内会新建对象。
/**
* 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)
//注意i + (-IntegerCache.low)的理解,比如缓存区为-128到127,对应的索引为0到225,因此你传入i为10的话,从中取值的索引就应该为10+127=137,这样对应的正好是10的值。
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
经典案例
根据如果传入的int整数在缓冲区内(注意这个缓冲区是-128到127或者-128到你在VM中设置的值)就直接取出,也就是冲常量池中取出,否则就新建一个对象,注意这里是重点,不在缓冲区内会新建对象。解决以下经典案例
默认情况下,缓冲区数据为-128到127
public class Test {
@org.junit.Test
public void test01(){
Integer i1 =127;//自动装箱
Integer i2 =127;
// 默认情况下,缓冲区数据为-128到127,因此直接取出值,返回true
System.out.println(i1==i2);//true
Integer i3 =128;//自动装箱
Integer i4 =128;
// 默认情况下,缓冲区数据为-128到127,128要新建对象,返回false
System.out.println(i3==i4);//false
}
}
VM中更改最大值后
public class Test {
@org.junit.Test
public void test01(){
Integer i1 =127;//自动装箱
Integer i2 =127;
// VM设置最大值-XX:AutoBoxCacheMax=128,缓冲区数据为-128到128,因此直接取出值,返回true
System.out.println(i1==i2);//true
Integer i3 =128;//自动装箱
Integer i4 =128;
// VM设置最大值-XX:AutoBoxCacheMax=128,缓冲区数据为-128到128,因此直接取出值,返回true
System.out.println(i3==i4);//true
}
}