今天学习的是自动装箱和自动拆箱
其实就是把Integer data=new Integer(10);
直接翻译成:Integer data=10;两个语句是一样的,装箱的功能是由编译器完成的。
public class AutoBox
{
public static void main(String[] args)
{
Integer data1=10;
Integer data2=20;
System.out.println(data1.doubleValue()/3);
System.out.println(data2.equals(data2));
}
}
拆箱:
Integer data1=10;
int data2=data1;//把data1转换成基本数据类型
小心使用箱子BOXING:
如:Integer i=null;
int j=i;
这样的语法在编译时期是合法的,但是在 运行时期就是错误的 ,因为这样写等于:
Integer i=null;
int j=i.intValue();
null表示i没有参与至任何对象实体,它可以合法的指定给对象参考名。所以也就不能操作intValue()方法,这样上面的写法运行时会出现NullPointerException异常。