内部Java实现,无法配置,范围是 -128到127 。您可以检查
Javadocs或仅查看来源:
public static Integer valueOf(int i) { final int offset = 128; if (i >= -128 && i <= 127) { // must cache return IntegerCache.cache[i + offset]; } return new Integer(i);}UPD。 我 错了 (感谢Marco Topolnik)。以上所有内容都与较旧的Java实现有关。对于 Java 7
实现,可以使用系统属性来实现:
-Djava.lang.Integer.IntegerCache.high=<size>
或JVM设置:
-XX:AutoBoxCacheMax=<size>
UPD。 2
java.math.BigInteger具有值 -16 <= x <= 16的硬编码缓存。从来源:
private final static int MAX_ConSTANT = 16; private static BigInteger posConst[] = new BigInteger[MAX_CONSTANT+1]; private static BigInteger negConst[] = new BigInteger[MAX_CONSTANT+1]; static { for (int i = 1; i <= MAX_CONSTANT; i++) { int[] magnitude = new int[1]; magnitude[0] = i; posConst[i] = new BigInteger(magnitude, 1); negConst[i] = new BigInteger(magnitude, -1); } } public static BigInteger valueOf(long val) { // If -MAX_ConSTANT < val < MAX_CONSTANT, return stashed constant if (val == 0) return ZERO; if (val > 0 && val <= MAX_CONSTANT) return posConst[(int) val]; else if (val < 0 && val >= -MAX_CONSTANT) return negConst[(int) -val]; return new BigInteger(val); }


