Java的自动装箱与拆箱:
原始基本数据类型:byte,short,char,int,long,float,double和boolean
他们对应的封装类:Byte,Short,Character,Integer,Long,Float,Double,Boolean。
自动装箱就是将基本数据类型自动转换为它们对应的封装类对象,自动拆箱就是封装类对象自动转换为它们对应的原始数据类型。
原始数据类型没有方法,它们对应的封装类对象有方法可以调用。
自动装箱主要发生在两种情况,一种是赋值时,另一种是在方法调用的时候。
public
static
Integer show(Integer iParam){
System.out.println(
"autoboxing example - method invocation i: "
+ iParam);
return
iParam;
}
//autoboxing and unboxing in method invocation
show(
3
);
//autoboxing
int
result = show(
3
);
//unboxing because return type of method is Integer
自动装箱:Integer i = 100;
自动拆箱:int t=i;