Integer对象
- 内部静态类IntegerCache会缓存-128-127的值,
- equals比较的值
- hashcode返回的是值
- compareTo比较的值 ```java package java.lang;
import java.lang.annotation.Native;
public final class Integer extends Number implements Comparable
/*** 缓存范围 -128 and 127 (inclusive) as required by JLS.** 可以通过参数调节 {@code -XX:AutoBoxCacheMax=<size>} option.*/private static class IntegerCache {static final int low = -128;static final int high;static final Integer cache[];static {// high value may be configured by propertyint h = 127;//省略通过获取参数来调节h的代码,可以大于等于127 ,小于127则取127high = 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() {}}/*** 判断是否属于缓存范围内,如果属于则从缓存中获取Integer对象*/public static Integer valueOf(int i) {if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);}/*** Integer hashcode 返回的是原值*/@Overridepublic int hashCode() {return Integer.hashCode(value);}public static int hashCode(int value) {return value;}/***使用Integer.equals()进行比较时,比较的是值*/public boolean equals(Object obj) {if (obj instanceof Integer) {return value == ((Integer)obj).intValue();}return false;}/*** 使用Integer.compareTo()进行比较时,比较的是值.*/public int compareTo(Integer anotherInteger) {return compare(this.value, anotherInteger.value);}public static int compare(int x, int y) {return (x < y) ? -1 : ((x == y) ? 0 : 1);}/** use serialVersionUID from JDK 1.0.2 for interoperability */@Native private static final long serialVersionUID = 1360826667806852920L;
}
hashcode
如果equals()返回true,则2个对象的哈希码必须相同,如果equals()返回false,则应该(但不是必需)不同。同样通过Object.hashCode()的声明,它必须是int。理想情况下,哈希码应该依赖于散列的所有数据。 PLASHHOLDER_FOR_CODE_10的哈希码 Long必须将8个字节映射到4个字节(int的大小)。 Long.hashCode()的当前实现只会返回i(如果它适合int),否则它将与高32位(4字节)进行异或运算
