java Integer

 自动装箱规范要求boolean,byte,char <= 127,介于 - 128~127之间的short和int被包装到固定的对象中。

 两个包装器对象比较内容时调用equals方法

	public static strictfp void main(String[] args) {
		/*
		 * Integer  -128 ~ 127 相等
		 * 其他不相等
         * int-->Integer  自动装箱  会调用Integer的valueOf方法
         * public static Integer valueOf(int i) {
         *       if (i >= IntegerCache.low && i <= IntegerCache.high)
         *        return IntegerCache.cache[i + (-IntegerCache.low)];
         *      return new Integer(i);
         *  }   
		 */
		Integer a = 100;
		Integer b = 100;
		if(a==b){
			System.out.println("相等");
		}else{
			System.out.println("不相等");
		}
		
		Integer c = 1000;
		Integer d = 1000;
		if(c==d){
			System.out.println("相等");
		}else{
			System.out.println("不相等");
		}
		
		Integer e = 1000;
		Integer f = 1000;
		if(e.equals(f)){
			System.out.println("相等");
		}else{
			System.out.println("不相等");
		}
	}

结果: 

	public static void main(String[] args) {
		
		/**
		 * 结果为true
		 * 当int和Integer进行==比较的时候,Java会把Integer进行自动拆箱,
		 * 调用intValue()方法,也就是把Integer转成int类型,所以这里进行比较的是int类型的值
		 */
		Integer i1 = new Integer(1000);
		int i2 = 1000;
		System.out.println(i1==i2);
		
		
		/*结果为false
		 * new出来的对象,并不是用的缓存
		 */
		Integer i3 = new Integer(10);
		Integer i4 = new Integer(10);
		System.out.println(i3==i4);		
		
	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值