• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java拆装箱深度剖析

java 搞代码 4年前 (2022-01-09) 13次浏览 已收录 0个评论

先来看一段代码:

public class Main{  public static void main(String[] args){     Integer num1 = 100;    Integer num2 = 100;    Integer num3 = 200;    Integer num4 = 200;     '''//输出结果'''    System.out.println(num1==num2);    System.out.println(num3==num4);  }}

猜猜结果是什么?

很多人都会认为结果全为true,但结果去不是这样的

true
false

为什么是这样的结果?如果用内存来解释结果的话,num1和num2指向的是同一个对象,而num3和num4则指向的确是不同的对象。接下来就告诉你为什么,看一看Integer类型的valueof方法的源码:

public static Integer valueOf(int i) {  asse<b>本文来源gao@!dai!ma.com搞$$代^@码!网</b>rt IntegerCache.high >= 127;  if (i >= IntegerCache.low && i <= IntegerCache.high)    return IntegerCache.cache[i + 128];  return new Integer(i);  }

其中IntegerCache的实现:

'''// IntegerCache,一个内部类,注意它的属性都是定义为static final'''  private static class IntegerCache {    static final int high; '''//缓存上界,暂为null'''    static final Integer cache[]; '''//缓存的整型数组'''     '''// 块,为什么定义为块'''    static {      final int low = -128; '''// 缓存下界,不可变了。只有上界可以改变'''       '''// high value may be configured by property'''      '''// h值,可以通过设置jdk的AutoBoxCacheMax参数调整(以下有解释),自动缓存区间设置为[-128,N]。注意区间的下界是固定'''      int h = 127;       if (integerCacheHighPropValue != null) {        '''// Use Long.decode here to avoid invoking methods that'''        '''// require Integer's autoboxing cache to be initialized'''        // 通过解码integerCacheHighPropValue,而得到一个候选的上界值'''        int i = Long.decode(integerCacheHighPropValue).intValue();        '''// 取较大的作为上界,但又不能大于Integer的边界MAX_VALUE'''        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++); '''// -128到high值逐一分配到缓存数组'''    }     private IntegerCache() {}  }

通过这两段代码可以看出,在通过valueof方法创建Integer类型对象时,取值范围为[-128,127],数值在这个区间里,指针指向IntegerCache.cache中已经存在的对象引用,当数值超出这个范围,就会创建一个新的对象。

有一点需要注意的是,并不是所有的类型都是这个范围,看Double类型:

public class Main{  public static void main(String[] args){     Double i1 = 100.0;    Double i2 = 100.0;    Double i3 = 200.0;    Double i4 = 200.0;     System.out.println(i1==i2);    System.out.println(i3==i4);  }}

最终的输出结果:

false
false

具体为什么回事这样的结果,大伙可以去看看源代码中Double valueof方法的实现,其和Integer valueof方法不同,是因为在某个范围内的整型数值的个数是有限的,而浮点数却不是。

注意,Integer、Short、Byte、Character、Long这几个类的valueOf方法的实现是类似的。
Double、Float的valueOf方法的实现是类似的。

拉下了一个,Boolean类型的结果有两个True or False。直接看源代码:

public static Boolean valueOf(boolean b) {    return (b ? TRUE : FALSE);  }

而其中的TRUE和FALSE是这样定义的:

public static final Boolean TRUE = new Boolean(true); '''/** ''''''* The <code>Boolean</code> object corresponding to the primitive ''''''* value <code>false</code>. ''''''*/'''public static final Boolean FALSE = new Boolean(false);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持搞代码

更多Java拆装箱深度剖析相关文章请关注搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java拆装箱深度剖析

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址