【java基础】自动装箱&自动拆箱&缓存

这是我参与更文挑战的第3天,活动详情查看: 更文挑战

介绍

基本数据类型的自动装箱(autoboxing)、拆箱(unboxing)是自J2SE 5.0开始提供的功能。 这些都是编译器为我们做的事情。

  • 自动装箱,将基本类型⽤它们对应的引⽤类型包装起来

例如,把int类型 转换成 Integer类型

  • 自动拆箱,将包装类型转换为基本数据类型;

例如,把Integer类型转换成int类型

例子

首先看一段代码,大家可以先分析下运行结果,然后再看看与实际的结果是否一样。

   
       
    public static void main(String[] args) {
        Integer i1 = new Integer(1);
        Integer i2 = new Integer(1);
        System.out.println(i1 == i2);
        
        //-------------------------------------------    
        Integer i3 = 127;  
        Integer i4 = 127;
        int i5 = 127;

        System.out.println(i3 == i4);
        System.out.println(i3 == i5);

       //-------------------------------------------    
        Integer i6 = 128;
        Integer i7 = 128;
        System.out.println(i6 == i7);
    }


复制代码

解析

        Integer i1 = new Integer(1);
        Integer i2 = new Integer(1);
        System.out.println(i1 == i2); //false
复制代码

由于new了两个对象,比较的是它们的对象的地址,所以结果为false;

        Integer i3 = 127;  
        Integer i4 = 127;
        int i5 = 127;

        System.out.println(i3 == i4); //true
        System.out.println(i3 == i5); //true
        
        Integer i6 = 128;
        Integer i7 = 128;
        System.out.println(i6 == i7); //false
复制代码
  • i3 == i4;

对于Integer i3=127;是基本数据类型赋值给引用类型,有一个自动装箱的操作,系统执行了 Integer.valueOf(int i),来看下它的一部分源码可以知道对于–128到127(默认是127)之间的值,Integer.valueOf(int i) 返回的是缓存的Integer对象(并不是新建对象)所以i3 == i5的结果为true

  • i6 == i7
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
复制代码

128已经超出了缓存的范围所以需要new对象,那么对于两个对象比较的是地址值所以i6 == i7的结果为false

  • i3 == i5

引用数据类型Integer和基本数据类型int比较时会有一个自动拆箱的操作,系统调用了intValue() 方法把Integer类型转换为int类型,所以i3与i5比较的是数值,结果为true

Integer类相关的部分源码

 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() {}
    }
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享