Integer 是 int 包装类,int 是八大基本数据类型之一
Integer 默认值为null,int 默认值为0;
Integer 表示的是对象,用一个引用指向这个对象,而int是基本数据类型,直接存储数值。
自动拆箱和自动装箱,
其执行是在编译期,在生成class文件的时候,决定是否进行拆箱和装箱动作。
1、自动装箱
一般我们创建一个类的时候是通过new关键字,
比如:Object obj = new Object();
但是对于 Integer 类,我们却可以这样:Integer a = 123;
因为生成的class文件是:Integer a = Integer.valueOf(123);
这就是基本数据类型的自动装箱,123是基本数据类型,然后被解析成Integer类。
2、自动拆箱
我们将 Integer 类表示的数据赋值给基本数据类型int,就是自动拆箱。
Integer a = new Integer(123);
int x = a;
反编译生成的class文件:
Integer a = new Integer(123);
int x = a.intValue();
即:自动装箱就是Integer.valueOf(int j);自动拆箱就是 j.intValue();
接上面:你可能用到的情况
Integer[] dataInteger = {1,2,3,4,5};
int[] dataint = {1,2,3,4,5};
List listInteger = Arrays.asList(dataInteger);
List listint = Arrays.asList(dataint);
System.out.println("Integer列表中的元素数量是:" + listInteger.size());//5
System.out.println("int列表中的元素数量是:" + listint.size());//1