从源码角度解析128陷阱

Java中的Integer类在-128到127之间会缓存重复的对象,以提高性能。超出这个范围,每次都会创建新对象,这就是128陷阱。自动装箱和拆箱涉及Integer对象与基本类型int之间的转换,当比较Integer对象时,要注意可能存在的引用比较而非值比较。
摘要由CSDN通过智能技术生成

在分析128陷阱之前首先需要了解几个前置概念

包装器类型

由于在使用集合类(Set、Map、List)时需要有对象传入,而Java语言的8种基本数据类型(int、short、long、float、double、boolean、byte、char)放不进去,因此需要包装器类型对基本数据类型进行包装

自动拆箱

自动拆箱(Unboxing):将包装类对象转换为对应的基本数据类型。
自动拆箱通过xxxValue()实现,(xxx为基本数据类型)例子如下:

		Integer a=10;
		Integer b=a.intValue();
		System.out.println(a==b);

打印结果:true

自动装箱

自动装箱(Autoboxing):将基本数据类型转换为对应的包装类对象。
自动拆箱通过.valueOf()实现,例子如下:

		Integer c=10;
		Integer d=Integer.valueOf (10);
		System.out.println(c==d);

打印结果:true

了解以上概念之后我们来了解128陷阱

128陷阱实例:

		Integer num1 = -128;
		Integer num2 = -128;
		System.out.println(num1 == num2);
		打印结果:true
	
		Integer num1 = 127;
		Integer num2 = 127;
		System.out.println(num1 == num2);
		打印结果:true
		
		Integer num3 = 128;
		Integer num4 = 128;
		System.out.println(num3 == num4);
		打印结果:false

上述代码可以看出,当num取值位于-128-127时,打印的结果均为true,这说明num1、2对应地址一致,num3、4对应地址一致,而超出127之后,出现了地址不一致问题,接下来我们查看一下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;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                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);
            }
            high = h;

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

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

结论:128陷阱产生原因

Integer类在加载的时候会加载内部的静态类IntegerCache,该类在加载中会从-128-127构建一个Integer数组存起来,当我们使用自动装箱时会直接从数组中取对象使用,Integer a=10 和 Integer b=10指向的对象就是相同的对象此时返回true,当valueOf()超过该范围(-128-127)后,就会new Integer创建新的对象,此时a==b返回false

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 中,ImageView 是一个常用的控件,通常用于显示图片。其中的 setVisible 方法是用来设置 ImageView 是否可见的方法。下面从源码角度分析一下这个方法的实现。 ImageView 的 setVisible 方法实际上是从 View 类继承而来的,其源码如下: ``` @Override public void setVisible(boolean visible, boolean restart) { super.setVisible(visible, restart); mPrivateFlags &= ~PFLAG_DRAWABLE_STATE_DIRTY; } ``` 在这个方法中,首先调用了 View 类的 setVisible 方法,而该方法中的实现主要是设置 View 的 visibility 属性。而 ImageView 这个子类中,visibility 属性的设置方法是 setVisibility 方法。因此,如果想要设置 ImageView 的可见性,实际上应该调用的是 setVisibility 方法。 setVisible 方法的第二个参数 restart 表示是否需要重绘 View。在 ImageView 中,如果需要重绘,则会调用 ImageView 的 invalidate 方法,使其重绘。而在 invalidate 方法中,会标记 View 的状态为需要重绘,这个标记的状态就是 mPrivateFlags 变量中的 PFLAG_DRAWABLE_STATE_DIRTY 标记。因此,在 setVisible 方法中,还需要将这个标记清除,以便在下一次需要重绘时,重新标记。 综上所述,ImageView 的 setVisible 方法主要是继承自 View 类,并调用了其父类的 setVisible 方法,同时还需要清除重绘标记。如果想要设置 ImageView 的可见性,应该调用 setVisibility 方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值