Java基础类型拆装箱源码解析
JDK1.5开始 增加了自动拆装箱机制 例如Integer:
//自动装箱
Integer i = 5; //等价于
Integer i = Integer.valueOf(5);
//自动拆箱
int j = i; //等价于
int j = i.intValue();
类似还有其他的基本类型总结如下:
基础类型总结:
基础类型 | 包装类型 | 字节 | 默认缓存范围 |
---|---|---|---|
boolean | Boolean | 1or4 | [true, false] |
byte | Byte | 1 | [-128,127] |
char | Character | 2 | [0,127] |
short | Short | 2 | [-128,127] |
int | Integer | 4 | [-128,127] |
long | Long | 8 | [-128,127] |
float | Float | 4 | - |
double | Double | 8 | - |
实际源码分析(JDK8环境下):
int类型:
private static class IntegerCache {//使用IntegerCache缓存 默认用于存储[-128,127]int值
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127; //定义默认最大值
//读取当前虚拟机参数配置 主要读取通过-XX:AutoBoxCacheMax 设置的值
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
//数组最大长度不能超过Integer.MAX_VALUE 因为负数已经占了128个 所以要减去
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;
//循环创建Integer缓存数据
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() {}
}
默认使用IntegerCache静态类 存储 [-128,127] 的int值,
同时支持使用 -XX:AutoBoxCacheMax=n 设置默认创建的IntegerCache缓存范围为:[-128,n]
valueOf() 方法:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
当给定i在 [-128,127] 之间时直接使用IntegerCache数组中的数据作为返回,当超过范围时使用new 创建Integer对象。
intValue() 方法:
private final int value;
...
...
public int intValue() {
return value;
}
直接返回int 类型的value值;
char|byte|short|long 类型
基本与Integer相同 定义对应类型静态类 XXCache 并且都不支持自定义vm设置缓存值上限。
只是 char 的包装类型默认范围在 [0,127] 之间 ,byte | short | long 的包装类都在 [-128,127] 之间。
CharacterCache
private static class CharacterCache {
private CharacterCache(){}
static final Character cache[] = new Character[127 + 1];
static {
for (int i = 0; i < cache.length; i++)
cache[i] = new Character((char)i);
}
}
其余几种类似于ByteCache
private static class ByteCache {
private ByteCache(){}
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));
}
}
boolean类型
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code true}.
*/
public static final Boolean TRUE = new Boolean(true);
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code false}.
*/
public static final Boolean FALSE = new Boolean(false);
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
boolean的包装类中 包含两个已经创建好的类静态属性 TRUE 和 FALSE ,使用valueOf()方法时实际使用的是这两个静态属性。
补充说明: 为什么boolean基本类型所占字节数不确定
《Java虚拟机规范》给出了4个字节,和boolean数组1个字节的定义。
原因 : 在Java虚拟机中没有任何供boolean值专用的字节码指令,Java语言表达式所操作的boolean值,在编译之后都使用Java虚拟机中的int数据类型来代替 (4个字节的由来)
而boolean数组将会被编码成Java虚拟机的byte数组,每个元素boolean元素占8位 (1个字节的由来)
float|double 类型
public static Float valueOf(float f) {
return new Float(f);
}
public static Double valueOf(double d) {
return new Double(d);
}
这两种基本类型的包装类 并没有缓存数据,调用valueOf()方法每次都是new 出对应的包装类型,原因也比较好理解,毕竟带小数,不是整数没法精确的指定要缓存的内容。
以上就是自动拆装箱的原理,好记性不如烂笔头~~~