Java中的自动拆装箱原理
概念
自动装箱:将基本数据类型自动包装为对应的包装类型。
自动拆箱:将包装类型自动拆解为对应的基本数据类型。
原理
public class IntegerTest {public static void main(String[] args) {Integer i =10;//自动装箱int a=i;//自动拆箱}
}
我们将这段代码使用JAD反编译工具反编译一下,代码如下:
public class IntegerTest
{public IntegerTest(){}public static void main(String args[]){Integer i = Integer.valueOf(10);int a = i.intValue();}
}
由代码看出。自动装箱其实是调用了valueOf()方法,自动拆箱调用了intValue方法。