1. 程式人生 > 程式設計 >Java包裝類的快取機制原理例項詳解

Java包裝類的快取機制原理例項詳解

這篇文章主要介紹了Java包裝類的快取機制原理例項詳解,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

java 包裝類的快取機制,是在Java 5中引入的一個有助於節省記憶體、提高效能的功能,只有在自動裝箱時有效

Integer包裝類

舉個栗子:

Integer a = 127;
Integer b = 127;
System.out.println(a == b);

這段程式碼輸出的結果為true

使用自動裝箱將基本型別轉為封裝類物件這個過程其實底層實現是呼叫封裝類的valueOf方法:

Integer a =127; 相當於 Integer a = Integer.valueOf(127);

看一下Integer的valueOf方法:

public static Integer valueOf(int i) {
  if (i >= IntegerCache.low && i <= IntegerCache.high)
    return IntegerCache.cache[i + (-IntegerCache.low)];
  return new Integer(i);
}

如果入參 i 大於等於IntegerCache.low或者小於等於IntegerCache.high),就從IntegerCache中獲取物件

看一下IntegerCache:

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() {}
}

預設範圍為:-128到127之間,範圍的最大值可以通過java.lang.Integer.IntegerCache.high設定,通過for迴圈將範圍內的資料例項化為Integer物件放到cache數組裡

在測試一下:

Integer a = 128;
Integer b = 128;
System.out.println(a == b);

輸出結果為false,所以如果沒有指定cache最大值時,在-128到127之間使用自動裝箱時,會使用快取

Byte包裝類

再舉個栗子:

public static void main(String[] args) {
  Byte a = 127;
  Byte b = 127;
  System.out.println(a == b); //true
}

由於Byte範圍在-128到127之間,所以Byte的valueOf都是從ByteCache快取中獲取的

public static Byte valueOf(byte b) {
  final int offset = 128;
  return ByteCache.cache[(int)b + offset];
}

ByteCache類:

private static class ByteCache {
  private ByteCache(){}

  static final Byte cache[] = new Byte[-(-128) + 127 + 1];

  static {
    for(int i = 0; i < cache.length; i++)
      cache[i] = new Byte((byte)(i - 128));
  }
}

與IntegerCache相比,ByteCache的最大值是不能修改的就是127

Short包裝類

public static Short valueOf(short s) {
  final int offset = 128;
  int sAsInt = s;
  if (sAsInt >= -128 && sAsInt <= 127) { // must cache
    return ShortCache.cache[sAsInt + offset];
  }
  return new Short(s);
}

ShortCache類:

private static class ShortCache {
  private ShortCache(){}

  static final Short cache[] = new Short[-(-128) + 127 + 1];

  static {
    for(int i = 0; i < cache.length; i++)
      cache[i] = new Short((short)(i - 128));
  }
}

ShortCache的最大值也不可以修改,範圍只能在-128 ~ 127之間

Long包裝類的valueOf方法和LongCache類與Short包裝類的實現一致,範圍也是隻能在-128 ~ 127之間

Character包裝類

valueOf方法:

public static Character valueOf(char c) {
  if (c <= 127) { // must cache
    return CharacterCache.cache[(int)c];
  }
  return new Character(c);
}

CharacterCache類:

private static class CharacterCache {
  private CharacterCache(){}

  static final Character cache[] = new Character[127 + 1];

  static {
    for (int i = 0; i < cache.length; i++)
      cache[i] = new Character((char)i);
  }
}

Character的快取範圍在0 ~ 127之間

Boolean包裝類

valueOf方法:

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

TRUE跟FALSE都是static final修飾的靜態變數

public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);

Float包裝類 & Double包裝類

valueOf方法:

public static Float valueOf(float f) {
  return new Float(f);
}
public static Double valueOf(double d) {
  return new Double(d);
}

Float和Double沒有使用快取,直接new的物件

總結:

java的包裝類中:Byte,Short,Integer,Long,Character使用static程式碼塊進行初始化快取,其中Integer的最大值可以通過java.lang.Integer.IntegerCache.high設定;Boolean使用static final例項化的物件;Float和Double直接new的物件沒有使用快取

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。