Integer和int大小比较

来源:互联网 发布:洛阳管家婆软件 编辑:程序博客网 时间:2024/05/24 05:29

代码:

public class b {public static void main(String[] args){Integer a1=new Integer(1000);Integer a2=new Integer(1000);Integer a3=new Integer(50);Integer a4=new Integer(50);Integer b1=1000;Integer b2=1000;Integer b3=50;Integer b4=50;int c1=1000;int c2=50;System.out.println(a1==a2);//false --new出的对象和缓存不相干System.out.println(a3==a4);//fasleSystem.out.println(a1==b1);//falseSystem.out.println(a3==b3);//falseSystem.out.println(b1==b2);//falseSystem.out.println(b3==b4);//true--(-128-127由于经常用到,会被缓存起来 )缓存,类似于string中的字面量System.out.println(a1==c1);//true --Integer和int比较会自动拆箱System.out.println(a3==c2);//trueSystem.out.println(b1==c1);//trueSystem.out.println("--------");    String string="hello";    String string2="he"+new String("llo");     System.out.println(string==string2);}}

执行结果:

falsefalsefalsefalsefalsetruetruetruetrue--------false

主要注意下integer对象在-128和+127范围时的比较。

1 0
原创粉丝点击