Java中int和Integer关系是比较微妙的。关系如下:
1.int是基本的数据类型;
2.Integer是int的封装类;
3.int和Integer都可以表示某一个数值;
4.int和Integer不能够互用,因为他们两种不同的数据类型;
举例说明
ArrayList al=new ArrayList();
int n=40;
Integer nI=new Integer(n);
al.add(n);//不可以
al.add(nI);//可以
本文详细阐述了Java中int与Integer的区别,包括两者的数据类型特性、使用场景及转换方式。强调int为基本类型而Integer为其封装类,并通过实例展示了它们在集合中的使用区别。
Java中int和Integer关系是比较微妙的。关系如下:
1.int是基本的数据类型;
2.Integer是int的封装类;
3.int和Integer都可以表示某一个数值;
4.int和Integer不能够互用,因为他们两种不同的数据类型;
举例说明
ArrayList al=new ArrayList();
int n=40;
Integer nI=new Integer(n);
al.add(n);//不可以
al.add(nI);//可以
378
450

被折叠的 条评论
为什么被折叠?