最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java Integer.ValueOf()的一些了解

 更新時間:2020年05月07日 11:21:00   作者:kk的小跟班  
這篇文章主要介紹了Java Integer.ValueOf()的一些了解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本文是對 Integer.ValueOf()的一些了解,分享給大家

 這道題有的人或許做過,也可能選對,但是這其中的道理你卻不一定理解,在這里大牛走過,小白留下一起學(xué)習(xí)。

先來分析選型A,Integer i01 = 59,是一個裝箱的過程,在進行i01 == i02的比較過程中,因為右邊是整型,發(fā)生了拆箱的動作,所以進行了值得比較,所以返回true。

在這里拿出Integer a = 59,Integer b = 59,這種又會出現(xiàn)什么狀況呢,如果按照裝箱和拆箱來看就是true,如果按照對象來看,就是false,在你舉棋不定得時候你就應(yīng)該看看源碼了。

/**
   * Cache to support the object identity semantics of autoboxing for values between
   * -128 and 127 (inclusive) as required by JLS.
   *
   * The cache is initialized on first usage. The size of the cache
   * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
   * During VM initialization, java.lang.Integer.IntegerCache.high property
   * may be set and saved in the private system properties in the
   * sun.misc.VM class.
   */
 
  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) {
        try {
          int i = parseInt(integerCacheHighPropValue);
          i = Math.max(i, 127);
          // Maximum array size is Integer.MAX_VALUE
          h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
        } catch( NumberFormatException nfe) {
          // If the property cannot be parsed into an int, ignore it.
        }
      }
      high = h;
 
      cache = new Integer[(high - low) + 1];
      int j = low;
      for(int k = 0; k < cache.length; k++)
        cache[k] = new Integer(j++);
 
      // range [-128, 127] must be interned (JLS7 5.1.7)
      assert IntegerCache.high >= 127;
    }
 
    private IntegerCache() {}
  }

這個類是Integer類中的一個靜態(tài)內(nèi)部類,其中的靜態(tài)代碼塊在類進行加載的時候就進行了-127-128這些數(shù)字的創(chuàng)建和保存,將他們的引用全部保存在Cache數(shù)組中。

所以當(dāng)用Integer 聲明初始化變量時,會先判斷所賦值的大小是否在-128到127之間,若在,則利用靜態(tài)緩存中的空間并且返回對應(yīng)cache數(shù)組中對應(yīng)引用,存放到運行棧中,而不再重新開辟內(nèi)存。

這里你就懂了吧,Integer a = 59,Integer b = 59返回的就是true,Integer a = 300,Integer b = 300在判斷完之后就會new出來一個新的對象,所以會返回false。

我們來分析B選項,我們先來看Value的代碼。

* @param i an {@code int} value.
   * @return an {@code Integer} instance representing {@code i}.
   * @since 1.5
   */
  public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
      return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);

和上面的一樣,int進去之后首先進行判斷,如果在-128-127之間就會返回引用,否則就在堆上new出來對象。所以B選項返回true。

C選項i03返回的是Cache數(shù)組中的引用,而i04返回的是堆上對象的引用,所以返回的是false。

 System.out.println(i02== i04) i02是整型變量,i04是引用,這里又用到了解包,虛擬機會把i04指向的數(shù)據(jù)拆箱為整型變量再與之比較,所以比較的是數(shù)值,59==59,返回true.

到此這篇關(guān)于Java Integer.ValueOf()的一些了解的文章就介紹到這了,更多相關(guān)Java Integer.ValueOf()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

桑日县| 韶关市| 昌江| 新丰县| 万州区| 毕节市| 特克斯县| 昭苏县| 和硕县| 宝丰县| 南雄市| 疏勒县| 宜春市| 时尚| 岐山县| 阆中市| 枣阳市| 吉安市| 静海县| 金华市| 克山县| 大方县| 福建省| 昆山市| 凤庆县| 兴义市| 安阳市| 军事| 祥云县| 林州市| 长乐市| 德保县| 赤峰市| 阜宁县| 偃师市| 万荣县| 闵行区| 宁武县| 桐庐县| 平塘县| 寿阳县|