java包裝類的緩存機制

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

Integer包裝類數組

舉個栗子: 思考一下,下面的代碼會輸出什麼緩存

Integer a = 127;
Integer b = 127;
System.out.println(a == b);
Integer c = 128;
Integer d = 128;
System.out.println(c == d);
複製代碼

答案是: a == b輸出true; c == d輸出false性能

使用自動裝箱將基本類型轉爲封裝類對象這個過程其實底層實現是調用封裝類的valueOf方法:測試

Integer a =127; 至關於 Integer a = Integer.valueOf(127);spa

看一下Integer的valueOf方法:code

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中獲取對象orm

看一下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的對象沒有使用緩存

相關文章
相關標籤/搜索