128陷阱的源码分析

 先看构造缓存数组的这个方法,注意其中的静态代码块,即在调用Integer的时候,就已经执行了

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;
            /*//意味着可以通过JVM参数java.lang.Integer.IntegerCache.high来设置缓存的最大值*/
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            /*如果设置了最大值*/
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    /*最大值和127作比较,即即使我们设置了最大值,如果小于127,也不会生效*/
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    /*h为数组的最大值,这里是考虑边界情况,如果在jvm参数里设置的值超过边界才会执行,一般都是i*/
                    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;
            /*给cache数组中赋值*/
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            /*断言:即assert后的的布尔表达式永真才不报错,即保证high最小也得是127,要不然我就抛异常,其实还是边界处理*/
            assert IntegerCache.high >= 127;
        }
        private IntegerCache() {}
    }

再看一眼valueOf方法,就明白了什么是128了

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

	public static void main(String[] args) {
		int a = 10;
		int b = 200;
		Integer a1 = 10;
		Integer b1 = 200;
		Integer a2 = new Integer(a1);
		Integer b2 = new Integer(b1);
		System.out.println(a == a1);
		System.out.println(a == a2);
		System.out.println(b == b1);
		System.out.println(b == b2);
		System.out.println("==================");
		System.out.println(a1 == a2);
		System.out.println(b1 == b2);
		System.out.println("==================");
		Integer s1 = 10;
		Integer s2 = 10;
		Integer s3 = 233;
		Integer s4 = 233;
		System.out.println(s1 == s2);
		System.out.println(s3 == s4);
	}

}

前四行为true的原因:

        128陷阱只针对与Integer和Integer之间,当一个int和Integer作“==”运算的时候,Integer会自动拆箱,即作数值是否相等的运算。

接下来两行为false的原因:

        在赋值的时候,Integer a1 = 10,程序调用了自动装箱,ValueOf方法,即在127--128之间就返回在缓存数组的值,而如果不在返回的是新建对象在堆中的地址,因此a1中存放的是缓存数组中10的地址,而a2没有调用valueOf方法,调用的是Integer的构造函数,返回的是对象存放的地址, ”=="在非基本类型之间,比较的是地址是否相同,因此是false,与128陷阱无关。

最后两行为true,false的原因:

        128陷阱,s1,s2里存的都是缓存数组里10的地址,指向是相同的

        而233大于128,索引返回的是new ()对象,s3,s4中存放的是两个不同对象的地址,不相同一定是false。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值