码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA——装箱和拆箱

时间:2016-05-23 00:40:38      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

  Java  将某些基本数据类型自动转换为包装类型的过程称为装箱,相反自动将包装类型转换为基本数据类型的过程称为拆箱。

  Integer integer_1=1;  //装箱

  int i=integer_1;  //拆箱

  装箱会调用  Integer.valueOf(int)  函数:

  拆箱会调用  Integer.intValue(Integer)  函数


  几个面试中会经常遇到的问题:

  先来看一段代码:

 

  技术分享

  这段代码的输出结果是:

  技术分享

  对于第一行和第二行,因为装箱会调用vlueOf(),我们来看看这个函数的源码:

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

 

  IntegerCach的源码是:  

 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));
            }
            high = h;

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

        private IntegerCache() {}
    }

 

  从源码可以看出来这个函数内部是怎么处理装箱的,如果这个int值在-128-127之间,会返回一个已经定义好了的数组里面的元素,所以第一行是  true  ,第二行是  false。

  这里需要注意的是:除开浮点型的数值,Double和Float,其余的类型(当然包括Boolean类型)均是类似的操作,因为Double和Float的数值个数是无法统计的。如下:

Double double_1=27.0;
Double double_2=27.0;
System.out.println(double_1==double_2);

  可能开始你会想结果是true,其实结果是false:

 

  对于第三行,和第四行,如果==有一边存在算术表达式,则会拆箱,所以第3,4行均为true。

  对于第5行,equals函数不会改变对象类型,所以类型不一样们自然是false。

 

  另一个问题:

    Integer i=new Integer(1);  和  Integer i=1又和区别?

    前者不会触发装箱,后者会触发装箱,性能上和执行效率上,后者一般更优,但不是绝对的。

 

JAVA——装箱和拆箱

标签:

原文地址:http://www.cnblogs.com/pin-wang/p/5518240.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!