Java foundation - Data type caching resolver

Article Directory

Basic types of caching resolver

Integer caching resolver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private static class  {
static final int low = -128;
static final int high;
static final Integer cache[];
static {

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);
// Maximum array size is 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 () {}
}

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);
}

1, the use of automatic packing (Integer i = 1) way to create an Integer object, use valueOf be Integer object initialization, this time, will call IntegerCache.high, which is required for IntegerCache This static inner classes to initialize.


2, IntegerCache class has a cache array, when the loading IntegerCache, Integer object will be -128 to 127 are created and store into the cache array, and then determining whether the current value of the Integer object initialized to -128 between 127, and if so, taken directly from the cache buffer, and if not, the new a new Integer object.


3, after the re-create the Integer object using automatic packing way, values ​​taken directly from the cache when the cache between -128 to 127.

Therefore, Integer object created using an automatic packing way, when the two compared values ​​are equal so long as it is ture. Not between -128 to 127, when compared will a new object is new, resulting in the comparison result is false


Note : The minimum value Integer is fixed, only -128 and the highest value can be set via jvm parameters. In the implementation of java program plus -XX: AutoBoxCacheMax = Parameters.

Long and Byte, Character caching resolver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
}
}

public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}

Similarly Long caching mechanism (LongCache) and Integer, as well as Character (CharacterCache), Byte (ByteCache) caching mechanism is similar. However, only a maximum of Integer jvm parameters can be set, the others are fixed. Wherein, Byte, Short, Long range: -128 to 127; Character, the range is 0 to 127.


Follow me on micro-channel public number: FramePower
I occasionally releases related technology accumulation, welcomed the technical pursuit, like-minded friends to join and learn to grow together!


Micro-channel public number 1

Original: Big Box  Java foundation - Data type caching resolver


Guess you like

Origin www.cnblogs.com/petewell/p/11607218.html