您的位置:首页 > 编程语言 > Java开发

JAVA——装箱和拆箱

2016-05-22 23:01 357 查看
Java  将某些基本数据类型自动转换为包装类型的过程称为装箱,相反自动将包装类型转换为基本数据类型的过程称为拆箱。
  Integer integer_1=1;  //装箱

  int i=integer_1;  //拆箱

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

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

  几个面试中会经常遇到的问题:
  先来看一段代码:

  


  这段代码的输出结果是:

  


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

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又和区别?

    前者不会触发装箱,后者会触发装箱,性能上和执行效率上,后者一般更优,但不是绝对的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: