Integer自动装箱和拆箱,以及不使用不使用new关键字直接赋值会遇到的的问题

Integer直接赋值使用==判断是否相等

//java中如果Integer不是new出Integer对象,而是直接赋值如
		Integer a=100;
		Integer b=100;

		System.out.println(a==b);  //true
		
		Integer c=150; Integer d=150;
		
		System.out.println(c==d); //false

那么这里为什么会第一个打印true第二个打印false呢?
出现这样结果的原因要从Integer的自动装箱和拆箱来说起。

//Integer类会自动将int类型装箱为Integer
Integer i = 100;
//这行代码Integer会自动拆箱为int
int n = i;

装箱和拆箱成为了Java自身做的工作,将方法的调用隐藏了起来,其实装箱和拆箱的过程分别调用了Integer的valueOf()和intValue()方法,首先来看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的装箱用到了 IntegerCache,这里我们还看不出什么,我们点进 IntegerCache看看

private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

可以看出其中的low的值是-128,high的值是127,而cache数组存储了从low至high递增的Integer。而当valueOf方法中,当传入的参数也就是装箱的int在low至high的范围内时,Integer会从cache数组中取出相应的缓存Integer对象返回。

回到最上面的问题,为什么第二个要打印false?

Integer c=150; Integer d=150;
		
		System.out.println(c==d); //false

这两行代码的150不在low至high之间,此时c和d都是新new的Integer对象,是两个对象,所以使用==是不能判断两个的值是否相等的

接下来我们就看一看拆箱
我们先看一下源码

/**
     * Returns the value of this {@code Integer} as an
     * {@code int}.
     */
    public int intValue() {
        return value;
    }

这里就是直接返回的value值,这个就没什么说的了

总结
Integer的装箱会根据要装箱的int值大小选择从Integer的缓存中取出Integer对象或者新创建一个Integer对象返回,而Integer的自动拆箱会返回被装箱的int值,因为Integer的equals被重写过,建议Integer对象进行相等比较时使用equals方法。

本文参考
作者:Cloneable
链接:https://www.jianshu.com/p/2e4490738087

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值