----------------java中自动装箱、拆箱和包装类默认值问题-------------
装箱:
基本数据类型转换成包装类就是装箱(jdk1.5以后的特性,自动完成)。
拆箱:
把包装类转换成基本数据类型就是拆箱(jdk1.5以后的特性,自动完成)。
int和Integer之间的区别:
int是基本数据类型,Integer是包装类。
包装类中提供了很多对整数的操作方法。
int和Integer之间可以自动装箱和拆箱。
int的默认值是0。
Intege的默认值是null。
包装类在使用之前要判断是否为空。
例子:
publicclass Test4 {
publicstaticvoidmain(String[] args) {
Person p = new Person();
System.out.println(p.getId());
System.out.println(p.getAge());
//对象在使用之前要判断是否是空
Integer i = p.getAge()+ 10 ;
}
}
结果:
0
null
Exception in thread"main" null
java.lang.NullPointerException
at cn.tx.system.Test4.main(Test4.java:19)