星空网 > 软件开发 > Java

JAVA——装箱和拆箱

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

  Integer integer_1=1;  //装箱

  int i=integer_1;  //拆箱

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

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


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

  先来看一段代码:

 

  JAVA——装箱和拆箱

  这段代码的输出结果是:

  JAVA——装箱和拆箱

  对于第一行和第二行,因为装箱会调用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——装箱和拆箱

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

国内海运:https://www.goluckyvip.com/tag/89973.html
海运国内:https://www.goluckyvip.com/tag/89974.html
国内海运运输:https://www.goluckyvip.com/tag/89975.html
海运到马来西亚海运:https://www.goluckyvip.com/tag/89977.html
国内海运物流运输:https://www.goluckyvip.com/tag/89978.html
国内海运运输物流:https://www.goluckyvip.com/tag/89979.html
九月初新疆旅游服装搭配(新疆游玩必备衣服清单):https://www.vstour.cn/a/408257.html
黄果树瀑布景区景点 - 黄果树瀑布景区景点分布图:https://www.vstour.cn/a/408258.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流