/**
* 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 {@code -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.
*/
注释的翻译是(有道):*缓存支持对象标识语义的自动装箱之间的值
*根据JLS要求-128和127(含)
缓存在第一次使用时被初始化。缓存的大小
*可能由{@code-XX:AutoBoxCacheMax=}选项控制。
*在虚拟机初始化时,java.lang.Integer.IntegerCache.high属性
*可以设置和保存在私有系统属性在
* sun.misc.VM类。
privatestaticclassIntegerCache{staticfinalint low =-128;staticfinalint high;staticfinal Integer 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)assert IntegerCache.high >=127;}privateIntegerCache(){}}
/**
* Compares this object to the specified object. The result is
* {@code true} if and only if the argument is not
* {@code null} and is an {@code Integer} object that
* contains the same {@code int} value as this object.
*
* @param obj the object to compare with.
* @return {@code true} if the objects are the same;
* {@code false} otherwise.
*/publicbooleanequals(Object obj){if(obj instanceofInteger){return value ==((Integer)obj).intValue();}returnfalse;}
/**
* Returns a hash code for this {@code Integer}.
*
* @return a hash code value for this object, equal to the
* primitive {@code int} value represented by this
* {@code Integer} object.
*/@OverridepublicinthashCode(){return Integer.hashCode(value);}/**
* Returns a hash code for a {@code int} value; compatible with
* {@code Integer.hashCode()}.
*
* @param value the value to hash
* @since 1.8
*
* @return a hash code value for a {@code int} value.
*/publicstaticinthashCode(int value){return value;}