加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java拆装箱深度剖析

发布时间:2020-12-14 14:33:38 所属栏目:Java 来源:网络整理
导读:先来看一段代码: 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); }}

先来看一段代码:

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) {
  assert 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);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读