**
int
是一种基本数据类型,而Integer是相应于int的类类型,称为对象包装。
integer
是个泛型,它是一个封装类,里面封装的就是int这种基本数据类型。目地就是为了可以把这个创建它的对象。
另外,有些数据结构库类只能操作对象,而不支持基本数据类型的变量,包装类提供一种便利的方式,能够把基本数据类型转换成等价的对象,从而可以利用数据结构库类进行处理。
**
区别:
**
int类型是放在栈空间的,Integer是作为对象放在堆空间的;
int 是基本类型,不是类,为了符合面向对象编程,后来出现了Integer 类,他是对int进行封装的。
int不是对象,是java原始的数据类型,它默认值为0。
Integer是个对象,它有自己的方法,默认值为NULL。
自动装箱拆箱
//自动装箱
Integer total = 99;
//自定拆箱
int totalprim = total;
装箱就是自动将基本数据类型转换为包装器类型;拆箱就是自动将包装器类型转换为基本数据类型。
**
Integer和int之间的转换
**
int到Integer:
int a=1;
Integer A=new Integer(a);
或:
Integer A=Integer.valueOf(a);
Integer到int:
Integer A=new Integer(5);
int a=A.intValue();