緩存Integer等類型的頻繁使用的數據和對象,大幅度提升性能java

緩存Integer等類型的頻繁使用的數據和對象,大幅度提升性能(一道經典的Java筆試題)...

public class IntCacheDemo {

/**
* 一道經典的Java筆試面試題
*/
public static void main(String[] args) {
Integer a = 2013;
Integer b = 2013;
if (a == b) {
System.out.println("a==b");
} else if (a.equals(b)) {
System.out.println("a equals b");
}

Integer c = 24;
Integer d = 24;
if (c == d) {
System.out.println("c==d");
} else if (a.equals(b)) {
System.out.println("c equals d");
}
}

}

程序運行結果

a equals bc==d

解釋說明

Integer等包裝類型是引用類型,引用類型的對象之間的 == 比較,是比較兩個引用是否指向同一個對象.因此a == b不成立,a equals b 成立.

但是為什麼C == D也成立?

因為JDK內部默認會對-128到127之間的整數,進行緩存,兩個對象騎士是同一個,因此C和D這兩個引用指向同一個對象.

 /**
*緩存,為了支持從-128到127(包含)的自動包裝類型的“對象同一性語義”,這是Java語言規範所規定的。
*緩存,在第一次使用的時候初始化。緩存的大小可以通過“-XX:AutoBoxCacheMax=<size>”選項控制。
*在VM初始化期間,java.lang.Integer.IntegerCache.high屬性可以被設置和保存在私有的系統屬性sun.misc.VM class中。
*/

/**(擔心翻譯不夠準確,誤導大眾,特給出英文註釋)
* 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 -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.
*/

// IntegerCache,一個內部類,注意它的屬性都是定義為static final

private static class IntegerCache {

//緩存的下界,-128,不可變
static final int low = -128;

//緩存上界,暫為null
static final int high;

//緩存的整型數組

static final Integer cache[];

static {
// 緩存上屆,可以通過JVM屬性來配置
int h = 127;
String integerCacheHighPropValue = sun.misc.VM
.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
//最大的數組值是Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low));
}
high = h;

cache = new Integer[(high - low) + 1];
int j = low;
for (int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}

private IntegerCache() {
}
}

//128到127之間的是有緩存的
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
/<size>/<size>

為什麼上界high是可以配置的,而下屆low卻不能?

類似的內部緩存還有

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));
}
}
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));
}
}
private static class LongCache {
private LongCache(){}

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

static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Long(i - 128);
}
}
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);
}
}

代碼示例

public class AllCacheDemo { 

/**
* 演示JDK內部緩存
*/
public static void main(String[] args) {
Integer a = 28;
Integer b = 28;
println(a == b);

Byte c = 25;
Byte d = 25;
println(c==d);

Short p=12;
Short q=12;
println(p==q);

Long x=127L;
Long y=127L;
println(x==y);

Character m='M';
Character n='M';
println(m==n);
}

public static void println(Object o){
System.out.println(o);
}

}


分享到:


相關文章: