美文网首页
java基础之自动装箱

java基础之自动装箱

作者: DaneYang | 来源:发表于2018-04-20 15:32 被阅读0次

    转载:
    http://www.cnblogs.com/dolphin0520/p/3780005.html
    http://blog.csdn.net/chengzhezhijian/article/details/9628251

    一.什么是装箱?什么是拆箱?

    Java为每种基本数据类型都提供了对应的包装器类型。

    在Java SE5之前,如果要生成一个数值为10的Integer对象,必须这样进行:

    Integer i = new Integer(10);
    

    而在从Java SE5开始就提供了自动装箱的特性,如果要生成一个数值为10的Integer对象,只需要这样就可以了:

    Integer i = 10;
    

    这个过程中会自动根据数值创建对应的 Integer对象,这就是装箱

    那什么是拆箱呢?顾名思义,跟装箱对应,就是自动将包装器类型转换为基本数据类型:

    Integer i = 10;  //装箱
    int n = i;   //拆箱
    

    简单一点说:

    装箱就是自动将基本数据类型转换为包装器类型;

    拆箱就是自动将包装器类型转换为基本数据类型。

    下表是基本数据类型对应的包装器类型:

    基本数据 包装器类型
    int(4字节) Integer
    byte(1字节) Byte
    short(2字节) Short
    long(8字节) Long
    float(4字节) Float
    double(8字节) Double
    char(2字节) Character
    boolean(未定) Boolean

    二.装箱和拆箱是如何实现的?

    我们就以Interger类为例,下面看一段代码:

    public class Main {
        public static void main(String[] args) {
            Integer i = 10;
            int n = i;
        }
    }
    

    反编译class文件之后得到如下内容:

    public class Main
    {
      public static void main(String[] args)
      {
        Integer i = Integer.valueOf(10);
        int n = i.intValue();
      }
    }
    

    从反编译得到的字节码内容可以看出,在装箱的时候自动调用的是Integer的valueOf(int)方法。而在拆箱的时候自动调用的是Integer的intValue方法。

    其他的也类似,比如Double、Character。

    因此可以用一句话总结装箱和拆箱的实现过程:

    装箱过程是通过调用包装器的valueOf方法实现的,而拆箱过程是通过调用包装器的 xxxValue方法实现的。(xxx代表对应的基本数据类型)。

    三.面试中相关的问题

    1. 下面这段代码的输出结果是什么?
    public class Main {
        public static void main(String[] args) {
    
            Integer i1 = 100;
            Integer i2 = 100;
            Integer i3 = 200;
            Integer i4 = 200;
    
            System.out.println(i1==i2);
            System.out.println(i3==i4);
        }
    }
    

    也许有些朋友会说都会输出false,或者也有朋友会说都会输出true。但是事实上输出结果是:

    true
    false
    

    为什么会出现这样的结果?

    输出结果表明i1和i2指向的是同一个对象,而i3和i4指向的是不同的对象。

    此时只需一看源码便知究竟,下面这段代码是Integer的valueOf方法的具体实现:

    /**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
    

    而其中IntegerCache类的实现为:

    /**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */
    
    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) {
                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() {}
    }
    

    从这2段代码可以看出,在通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。

    The Java Language Specification, 3rd Edition 写道:

    If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

    为了节省内存,对于下列包装对象的两个实例,当它们的基本值相同时,他们总是==:

     Boolean:(全部缓存)
     Byte:(全部缓存)
     Character: (\u0000 - \u007f)(<= 127缓存)
     Short:(-128 — 127缓存)
     Long:(-128 — 127缓存)
     Float:(没有缓存)
    

    同样对于垃圾回收器来说:

    Integer i = 100;
    i = null;//will not make any object available for GC at all.
    

    这里的代码不会有对象符合垃圾回收器的条件,这儿的i虽然被赋予null,但它之前指向的是cache中的Integer对象,而cache没有被赋null,所以Integer(100)这个对象还是存在。

    而如果i大于127或小于-128则它所指向的对象将符合垃圾回收的条件:

    Integer i = 10000;
    i = null;//will make the newly created Integer object available for GC.
    

    使用Oracle/Sun JDK 6,在server模式下,使用-XX:AutoBoxCacheMax=NNN参数即可将Integer的自动缓存区间设置为[-128,NNN]。注意区间的下界固定在-128不可配置。

    在client模式下该参数无效。这个参数是server模式专有的,在c2_globals.hpp中声明,默认值是128;不过这个默认值在默认条件下不起作用,要手动设置它的值或者是开启-XX:+AggressiveOpts参数才起作用。

    在设置了-XX:+AggressiveOpts启动参数后,AutoBoxCacheMax的默认值会被修改为20000并且生效。

    参考arguments.cpp:

    // Aggressive optimization flags  -XX:+AggressiveOpts
    void Arguments::set_aggressive_opts_flags() {
    #ifdef COMPILER2
      if (AggressiveOpts || !FLAG_IS_DEFAULT(AutoBoxCacheMax)) {
        if (FLAG_IS_DEFAULT(EliminateAutoBox)) {
          FLAG_SET_DEFAULT(EliminateAutoBox, true);
        }
        if (FLAG_IS_DEFAULT(AutoBoxCacheMax)) {
          FLAG_SET_DEFAULT(AutoBoxCacheMax, 20000);
        }
    
        // Feed the cache size setting into the JDK
        char buffer[1024];
        sprintf(buffer, "java.lang.Integer.IntegerCache.high=" INTX_FORMAT, AutoBoxCacheMax);
        add_property(buffer);
      }
      // ...
    #endif
    }
    

    测试代码:

    // run with:
    // java -server -XX:AutoBoxCacheMax=1000 TestAutoBoxCache
    
    public class TestAutoBoxCache {
        public static void main(String[] args) {
            Integer a = 1000;
            Integer b = 1000;
            System.out.println(a == b);
    
            Integer c = 1001;
            Integer d = 1001;
            System.out.println(c == d);
    
            Integer e = 20000;
            Integer f = 20000;
            System.out.println(e == f);
        }
    }
    

    在命令行上测试:

    $ javac TestAutoBoxCache.java
    
    $ java TestAutoBoxCache
    false
    false
    false
    
    $ java -server TestAutoBoxCache
    false
    false
    false
    
    $ java -Djava.lang.Integer.IntegerCache.high=1000 TestAutoBoxCache
    true
    false
    false
    
    $ java -server -Djava.lang.Integer.IntegerCache.high=1000 TestAutoBoxCache
    true
    false
    false
    
    $ java -Djava.lang.Integer.IntegerCache.high=1001 TestAutoBoxCache
    true
    true
    false
    
    $ java -server -Djava.lang.Integer.IntegerCache.high=1001 TestAutoBoxCache
    true
    true
    false
    
    $ java -XX:AutoBoxCacheMax=1000 TestAutoBoxCache
    Unrecognized VM option 'AutoBoxCacheMax=1000'
    Could not create the Java virtual machine.
    
    $ java -server -XX:AutoBoxCacheMax=1000 TestAutoBoxCache
    true
    false
    false
    
    $ java -server -XX:AutoBoxCacheMax=1001 TestAutoBoxCache
    true
    true
    false
    
    $ java -server -XX:+AggressiveOpts TestAutoBoxCache
    true
    true
    true
    

    中间报Unrecognized VM option 'AutoBoxCacheMax=1000'错误是因为这个参数只能在HotSpot Server VM上使用,在HotSpot Client VM上不支持。

    1. 下面程序的输出结果是什么?
    public class Main {
        public static void main(String[] args) {
    
            Integer a = 1;
            Integer b = 2;
            Integer c = 3;
            Integer d = 3;
            Integer e = 321;
            Integer f = 321;
            Long g = 3L;
            Long h = 2L;
    
            System.out.println(c==d);
            System.out.println(e==f);
            System.out.println(c==(a+b));
            System.out.println(c.equals(a+b));
            System.out.println(g==(a+b));
            System.out.println(g.equals(a+b));
            System.out.println(g.equals(a+h));
        }
    }
    
    

    运行结果:

    true
    false
    true
    true
    true
    false
    true
    

    这里面需要注意的是:当 “==”运算符的两个操作数都是 包装器类型的引用,则是比较指向的是否是同一个对象,而如果其中有一个操作数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程)。

    另外,对于包装器类型,equals方法并不会进行类型转换

    相关文章

      网友评论

          本文标题:java基础之自动装箱

          本文链接:https://www.haomeiwen.com/subject/eatzkftx.html