- 包装类型
我们已经知道,Java的数据类型分两种:
- 基本类型:
byte,short,int,long,boolean,float,double,char;
- 引用类型:所有
class和interface类型。
引用类型可以赋值为
null,表示空,但基本类型不能赋值为null:String s = null; int n = null; // compile error!
那么,如何把一个基本类型视为对象(引用类型)?
比如,想要把
int基本类型变成一个引用类型,我们可以定义一个Integer类,它只包含一个实例字段int,这样,Integer类就可以视为int的包装类(Wrapper Class):