Integer和int大小比较

代码:

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);//fasle
		System.out.println(a1==b1);//false
		System.out.println(a3==b3);//false
		System.out.println(b1==b2);//false
		System.out.println(b3==b4);//true--(-128-127由于经常用到,会被缓存起来 )缓存,类似于string中的字面量
		
		System.out.println(a1==c1);//true --Integer和int比较会自动拆箱
		System.out.println(a3==c2);//true
		System.out.println(b1==c1);//true
		System.out.println("--------");
	    String string="hello";
	    String string2="he"+new String("llo"); 
	    System.out.println(string==string2);
	}
}

执行结果:

false
false
false
false
false
true
true
true
true
--------
false

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值