java---------128陷阱

java---------128陷阱

public class Test {
	public static void main(String []agrs) {
		Integer a=100;
		Integer b=100;
		System.out.println(a==b);
		a=1000;
		b=1000;
		System.out.println(a==b);
	}
}

第一个会输出true,而第二个会输出false,这是为什么呢?

在Java中有一个128陷阱,当数据的绝对值小于某个值时,在进行比较时java会自动进行拆箱操作,但是当数据的绝对值大于这个值时,就不会进行自动拆箱操作了,需要我们进行手动拆箱

Integer a = 100;

Java会自动将上面的这句话转化为下面的这句

Integer a = Integer.valueOf(100);

但是当超过某个值时就需要我们手动执行valueOf()这个方法了
我们来看看valueOf()方法里都有什么

public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

显然,当i >= IntegerCache.low && i <= IntegerCache.high时,会返回cache数组中现有的Integer对象,但是当不满足这个条件时就会创建一个新的Integer对象

IntegerCache.lowIntegerCache.high这两个东西又是什么呢?

接下来我们来看看IntegerCache这个类里都有什么

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

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

            // Load IntegerCache.archivedCache from archive, if possible
            VM.initializeFromArchive(IntegerCache.class);
            int size = (high - low) + 1;

            // Use the archived cache if it exists and is large enough
            if (archivedCache == null || size > archivedCache.length) {
                Integer[] c = new Integer[size];
                int j = low;
                for(int i = 0; i < c.length; i++) {
                    c[i] = new Integer(j++);
                }
                archivedCache = c;
            }
            cache = archivedCache;
            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

刚刚我们一直在说某个值,那么这个值是什么呢,这里就有了答案,我们看到最低数值等于-128,而最大数值等于127,所以当数值在这个范围里面的时候,就会直接返回cache数组中现有的Integer对象,但是当数值不在这个范围里面就会创建一个新的对象

而我们知道,==比较的是内存地址,如果直接返回cache数组中的对象,那么就会是同一个内存地址,但是如果是新创建的对象,就会有不同的内存地址

就像下面这样
在这里插入图片描述
这样大家就明白了吧

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值