java包裝類的緩存機制(轉)


出處: java包裝類的緩存機制

 

java 包裝類的緩存機制,是在Java 5中引入的一個有助於節省內存、提高性能的功能,只有在自動裝箱時有效

 

Integer包裝類

舉個栗子:

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

這段代碼輸出的結果為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

輸出結果為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的對象沒有使用緩存

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM