首頁  >  文章  >  Java  >  Java拆裝箱深度剖析

Java拆裝箱深度剖析

高洛峰
高洛峰原創
2017-01-24 13:57:281127瀏覽

先來看一段程式碼:

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,但結果去不是這樣的

tru​​e
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的實作:

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

   

其中IntegerCache的實作:

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);
  }
}

   

valueof方法建立Integer型別物件時,取值範圍為[-128,127],數值在這個區間裡,指標指向IntegerCache.cache中已經存在的物件引用,當數值超出這個範圍,就會建立一個新的物件。

有一點要注意的是,並不是所有的類型都是這個範圍,看Double類型:

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

   


最終的輸出結果:

false

false
,大夥可以去看看原始碼中Double valueof方法的實現,其和Integer valueof方法不同,是因為在某個範圍內的整數數值的個數是有限的,而浮點數卻不是。

注意,Integer、Short、Byte、Character、Long這幾個類別的valueOf方法的實作是類似的。 

Double、Float的valueOf方法的實作是類似的。


拉下了一個,Boolean類型的結果有兩個True or False。直接看原始碼:

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

   


而其中的TRUE和FALSE是這樣定義的:

rrreee

   

大家多多支援PHP中文網。


更多Java拆裝箱深度剖析相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn