int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象
获取到integer必须先判null再转int
public Integer getContactType() { return contactType; } public String getContactTypeStr() { List<String> contactStrings = Arrays.asList(App.getContext().getResources().getStringArray(R.array.contactArr)); if (getContactType() != null) { int a = getContactType().intValue(); return contactStrings.get(a); } else { return "没有该数据"; } }Java中每种内建类型都有相应的外覆类。
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);//可以
并且泛型定义时也不支持int: 如:List<Integer> list = new ArrayList<Integer>();可以 而List<int> list = new ArrayList<int>();则不行
int与integer的相互转换
int到Integer:
int a=3;
Integer A=new Integer(a);
或:
Integer A=Integer.valueOf(a);
Integer到int:
Integer A=new Integer(5);
int a=A.intValue();
int类型是放在栈空间的,Integer是作为对象放在堆空间的;int 是基本类型,不是类,为了符合面向对象编程,后来出现了Integer 类,他是对int进行封装的.
int不是对象,是java原始的数据类型,它默认值为0.
Integer是个对象,它有自己的方法,默认值为NULL