欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java拆裝箱深度剖析

 更新時(shí)間:2016年12月30日 10:39:27   作者:令仔很忙  
這篇文章主要為大家深度剖析了Java拆箱裝箱的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

先來(lái)看一段代碼:

public class Main{
  public static void main(String[] args){

    Integer num1 = 100;
    Integer num2 = 100;
    Integer num3 = 200;
    Integer num4 = 200;

    '''//輸出結(jié)果'''
    System.out.println(num1==num2);
    System.out.println(num3==num4);
  }
}

猜猜結(jié)果是什么?

很多人都會(huì)認(rèn)為結(jié)果全為true,但結(jié)果去不是這樣的

true
false

為什么是這樣的結(jié)果?如果用內(nèi)存來(lái)解釋結(jié)果的話,num1和num2指向的是同一個(gè)對(duì)象,而num3和num4則指向的確是不同的對(duì)象。接下來(lái)就告訴你為什么,看一看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的實(shí)現(xiàn):

'''// IntegerCache,一個(gè)內(nèi)部類,注意它的屬性都是定義為static final'''
  private static class IntegerCache {
    static final int high; '''//緩存上界,暫為null'''
    static final Integer cache[]; '''//緩存的整型數(shù)組'''

    '''// 塊,為什么定義為塊'''
    static {
      final int low = -128; '''// 緩存下界,不可變了。只有上界可以改變'''

      '''// high value may be configured by property'''
      '''// h值,可以通過(guò)設(shè)置jdk的AutoBoxCacheMax參數(shù)調(diào)整(以下有解釋),自動(dòng)緩存區(qū)間設(shè)置為[-128,N]。注意區(qū)間的下界是固定'''
      int h = 127;

      if (integerCacheHighPropValue != null) {
        '''// Use Long.decode here to avoid invoking methods that'''
        '''// require Integer's autoboxing cache to be initialized'''
        // 通過(guò)解碼integerCacheHighPropValue,而得到一個(gè)候選的上界值'''
        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; '''//上界確定'''
      '''// 就可以創(chuàng)建緩存塊,注意緩存數(shù)組大小'''
      cache = new Integer[(high - low) + 1]; //
      int j = low;
      for(int k = 0; k < cache.length; k++)
        cache[k] = new Integer(j++); '''// -128到high值逐一分配到緩存數(shù)組'''
    }

    private IntegerCache() {}
  }

通過(guò)這兩段代碼可以看出,在通過(guò)valueof方法創(chuàng)建Integer類型對(duì)象時(shí),取值范圍為[-128,127],數(shù)值在這個(gè)區(qū)間里,指針指向IntegerCache.cache中已經(jīng)存在的對(duì)象引用,當(dāng)數(shù)值超出這個(gè)范圍,就會(huì)創(chuàng)建一個(gè)新的對(duì)象。

有一點(diǎn)需要注意的是,并不是所有的類型都是這個(gè)范圍,看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);
  }
}

最終的輸出結(jié)果:

false
false

具體為什么回事這樣的結(jié)果,大伙可以去看看源代碼中Double valueof方法的實(shí)現(xiàn),其和Integer valueof方法不同,是因?yàn)樵谀硞€(gè)范圍內(nèi)的整型數(shù)值的個(gè)數(shù)是有限的,而浮點(diǎn)數(shù)卻不是。

注意,Integer、Short、Byte、Character、Long這幾個(gè)類的valueOf方法的實(shí)現(xiàn)是類似的。
Double、Float的valueOf方法的實(shí)現(xiàn)是類似的。

拉下了一個(gè),Boolean類型的結(jié)果有兩個(gè)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);

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論