Integer的自动装箱与拆箱

Integer的自动装箱与拆箱

//Integer的自动装箱与拆箱
//JVM会自动缓存-128~127范围内的值 
public class IntegerDemo {
	public static void main(String[] args) {
		int a1=10;
		Integer a2=10;
		Integer a3=10;
		Integer a4=new Integer(10);
		
		int b1=1000;
		Integer b2=1000;
		Integer b3=1000;
		Integer b4=new Integer(1000);
		
		System.out.println(a1==a2);//true
		System.out.println(a1==a4);//true
		System.out.println(a2==a3);//true
		System.out.println(a2==a4);//false
		System.out.println("1--------");
		System.out.println(b1==b2);//true
		System.out.println(b1==b4);//true		
		System.out.println(b2==b3);	//false
		System.out.println(b2==b4);//false		
		System.out.println("2--------");
		
		Integer c1=127,c2=128;		
		int c3=128;		
		c1=c3;//装箱
		
		System.out.println(c1==c2);//false
		System.out.println(c1==c3);//true
		System.out.println(c2==c3);//true
		
		System.out.println("3-----------");
		
		Integer d1=127;
		Integer d2=127;
		
		System.out.println(d1==d2);//true
		
		d1=128;
		d2=128;
		
		System.out.println(d1==d2);//false		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值