1、Integer是int的包装类,int则是java的一种基本数据类型
2、Integer变量必须实例化才能使用,int变量不需要实例化
3、Integer的默认值是null,而int的默认值是0
4、Integer实际是一个对象的引用,当new一个Integer对象时,实际是生成一个指针指向该对象,而int是基本数据类型,直接存储数值
public class HelloWorld {
public static void main(String []args) {
int a=100;
int b=100;
int c=200;
int d=200;
Integer x=100;
Integer y=100;
Integer m=200;
Integer n=200;
System.out.println(a==b);//true
System.out.println(c==d);//true
System.out.println(x==y);//true
System.out.println(m==n);//false
System.out.println(a==x);//true
System.out.println(c==n);//true
}
}
在使用Integer时,对于-128到127之间的数,会进行缓存,Integer x=100时,会将100进行缓存,下次再写Integer y=100时,就会直接从缓存中取,不会新new一个Integer,所以 x和 y 用“==”进行比较时,会为true。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)