2、在 VM 初始化期间,java.lang.Integer.IntegerCache.high 属性可能会被设置并保存在 sun.misc.VM 类的私有系统属性中。
privatestaticclassIntegerCache{staticfinalint low =-128;staticfinalint high;staticfinalInteger cache[];static{// high value may be configured by propertyint 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 =newInteger[(high - low)+1];int j = low;for(int k =0; k < cache.length; k++)
cache[k]=newInteger(j++);// range [-128, 127] must be interned (JLS7 5.1.7)assertIntegerCache.high >=127;}privateIntegerCache(){}}// 自动装箱publicstaticIntegervalueOf(int i){if(i >=IntegerCache.low && i <=IntegerCache.high)returnIntegerCache.cache[i +(-IntegerCache.low)];returnnewInteger(i);}
2、LongCache
默认缓存 -128 到 127(包含)之间的值。
缓存会在首次使用时初始化。
自动装箱时,会用到。
privatestaticclassLongCache{privateLongCache(){}staticfinalLong cache[]=newLong[-(-128)+127+1];static{for(int i =0; i < cache.length; i++)
cache[i]=newLong(i -128);}}// 自动装箱publicstaticLongvalueOf(long l){finalint offset =128;if(l >=-128&& l <=127){// will cachereturnLongCache.cache[(int)l + offset];}returnnewLong(l);}
3、CharacterCache
默认缓存 0 到 127(包含)之间的值。
缓存会在首次使用时初始化。
自动装箱时,会用到。
privatestaticclassCharacterCache{privateCharacterCache(){}staticfinalCharacter cache[]=newCharacter[127+1];static{for(int i =0; i < cache.length; i++)
cache[i]=newCharacter((char)i);}}// 自动装箱publicstaticCharactervalueOf(char c){if(c <=127){// must cachereturnCharacterCache.cache[(int)c];}returnnewCharacter(c);}
4、ByteCache
默认缓存 -128 到 127(包含)之间的值。
缓存会在首次使用时初始化。
自动装箱时,会用到。
privatestaticclassByteCache{privateByteCache(){}staticfinalByte cache[]=newByte[-(-128)+127+1];static{for(int i =0; i < cache.length; i++)
cache[i]=newByte((byte)(i -128));}}// 自动装箱publicstaticBytevalueOf(byte b){finalint offset =128;returnByteCache.cache[(int)b + offset];}
5、ShortCache
默认缓存 -128 到 127(包含)之间的值。
缓存会在首次使用时初始化。
自动装箱时,会用到。
privatestaticclassShortCache{privateShortCache(){}staticfinalShort cache[]=newShort[-(-128)+127+1];static{for(int i =0; i < cache.length; i++)
cache[i]=newShort((short)(i -128));}}// 自动装箱publicstaticShortvalueOf(short s){finalint offset =128;int sAsInt = s;if(sAsInt >=-128&& sAsInt <=127){// must cachereturnShortCache.cache[sAsInt + offset];}returnnewShort(s);}
6、Boolean
通过静态常量,实现缓存 new Boolean(true) 和 new Boolean(false) 。
缓存会在首次使用时初始化。
自动装箱时,会用到。
publicfinalclassBooleanimplementsjava.io.Serializable,Comparable<Boolean>{/**
* The {@code Boolean} object corresponding to the primitive
* value {@code true}.
*/publicstaticfinalBooleanTRUE=newBoolean(true);/**
* The {@code Boolean} object corresponding to the primitive
* value {@code false}.
*/publicstaticfinalBooleanFALSE=newBoolean(false);publicstaticBooleanvalueOf(boolean b){return(b ?TRUE:FALSE);}publicstaticBooleanvalueOf(String s){returnparseBoolean(s)?TRUE:FALSE;}}
Integer x =newInteger(123);Integer y =newInteger(123);// falseSystem.out.println(x == y);Integer z =Integer.valueOf(123);Integer k =Integer.valueOf(123);// trueSystem.out.println(z == k);