自动拆装箱的原理

1.什么是自动拆装箱

自动装箱就是将java的基本类型,比如说int类型的变量转换成Integer对象,反之从引用类型转换成基本类型就是拆箱。

看一段代码以及它的输出结果:

public class Test {  
        public static void main(String[] args) {      
            test();  
        }  
    
        public static void test() {  
            int i = 40;  
            int i0 = 40;  
            Integer i1 = 40;  
            Integer i2 = 40;  
            Integer i3 = 0;  
            Integer i4 = new Integer(40);  
            Integer i5 = new Integer(40);  
            Integer i6 = new Integer(0);  
            Double d1=1.0;  
            Double d2=1.0;  
              
            System.out.println("i=i0\t" + (i == i0));  
            System.out.println("i1=i2\t" + (i1 == i2));  
            System.out.println("i1=i2+i3\t" + (i1 == i2 + i3));  
            System.out.println("i4=i5\t" + (i4 == i5));  
            System.out.println("i4=i5+i6\t" + (i4 == i5 + i6));      
            System.out.println("d1=d2\t" + (d1==d2));   
              
            System.out.println();          
        }  
    }

结果:

 输出的结果:
 i=i0          true
 i1=i2         true
 i1=i2+i3    true
 i4=i5        false
 i4=i5+i6    true
 d1=d2      false

 

2.自动拆装箱的原理

自动装箱的话,调用valueOf()方法将基本类型转换成引用类型,自动拆箱的话,编译器调用intValue()、doubleValue()这种方法。

    public static Integer valueOf(int i) {
        //判断i是否在-128和127之间,存在则从IntegerCache中获取包装类的实例,否则new一个新实例
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }


    //使用亨元模式,来减少对象的创建(亨元设计模式大家有必要了解一下,我认为是最简单的设计模式,也许大家经常在项目中使用,不知道他的名字而已)
    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        //静态方法,类加载的时候进行初始化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() {}
    }

我们初始化一个Integer对象的话,先判断值是否在-128-127之间,如果在就直接从IntegerCache.cache缓存中获取指定数字的包装类,如果不在的话new一个包装类出来。

IntegerCache内部实现了一个Integer的静态常量数组,在类加载的时候,执行static代码块初始化-128-127之间的Integer对象,存放到cache数组中。cache属于常量,存放在方法区中。

我们来看一下java8种基本类型的自动装箱:

    //boolean原生类型自动装箱成Boolean
    public static Boolean valueOf(boolean b) {
        return (b ? TRUE : FALSE);
    }

    //byte原生类型自动装箱成Byte
    public static Byte valueOf(byte b) {
        final int offset = 128;
        return ByteCache.cache[(int)b + offset];
    }

    //byte原生类型自动装箱成Byte
    public static Short valueOf(short s) {
        final int offset = 128;
        int sAsInt = s;
        if (sAsInt >= -128 && sAsInt <= 127) { // must cache
            return ShortCache.cache[sAsInt + offset];
        }
        return new Short(s);
    }

    //char原生类型自动装箱成Character
    public static Character valueOf(char c) {
        if (c <= 127) { // must cache
            return CharacterCache.cache[(int)c];
        }
        return new Character(c);
    }
    
    //int原生类型自动装箱成Integer
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

    //int原生类型自动装箱成Long
    public static Long valueOf(long l) {
        final int offset = 128;
        if (l >= -128 && l <= 127) { // will cache
            return LongCache.cache[(int)l + offset];
        }
        return new Long(l);
    }

    //double原生类型自动装箱成Double
    public static Double valueOf(double d) {
        return new Double(d);
    }

    //float原生类型自动装箱成Float
    public static Float valueOf(float f) {
        return new Float(f);
    }

通过分析源码发现,只有double和float的自动装箱代码没有使用缓存,每次都是new 新的对象,其它的6种基本类型都使用了缓存策略。

使用缓存策略是因为,缓存的这些对象都是经常使用到的(如字符、-128至127之间的数字),防止每次自动装箱都创建一次对象的实例。

而double、float是浮点型的,没有特别的热的(经常使用到的)数据的,缓存效果没有其它几种类型使用效率高。

再看几个自动拆箱的:

  //1、这个没解释的就是true
    System.out.println("i=i0\t" + (i == i0));  //true
    //2、int值只要在-128和127之间的自动装箱对象都从缓存中获取的,所以为true
    System.out.println("i1=i2\t" + (i1 == i2));  //true
    //3、涉及到数字的计算,就必须先拆箱成int再做加法运算,所以不管他们的值是否在-128和127之间,只要数字一样就为true
    System.out.println("i1=i2+i3\t" + (i1 == i2 + i3));//true  
    //比较的是对象内存地址,所以为false
    System.out.println("i4=i5\t" + (i4 == i5));  //false
    //5、同第3条解释,拆箱做加法运算,对比的是数字,所以为true
    System.out.println("i4=i5+i6\t" + (i4 == i5 + i6));//true      
    //double的装箱操作没有使用缓存,每次都是new Double,所以false
    System.out.println("d1=d2\t" + (d1==d2));//false

 

2023.10.22

1.

+=
-=

//代码一
byte a = 1;
byte b = 1;
b = a + b;
报错,运算的时候都转为int,没有进行类型转换

//代码二
byty a = 1;
byte b = 1;
a += b;
没问题,会自动转换(装箱)
posted @ 2021-12-12 16:13  sellingpear  阅读(241)  评论(0编辑  收藏  举报