类Integer具有缓存,缓存Integer值.因此,如果我使用方法valueOf或收件箱,新值将不会被实例化,而是从缓存中获取.
我知道默认缓存大小是127因为VM设置而可以扩展.我的问题是:在这些设置中缓存大小的默认值有多大,我可以操纵这个值吗?这个值取决于我使用的是哪个VM(32位还是64位)?
我现在正在调整遗留代码,可能需要从int转换为Integer.
澄清:遵循我在Java源代码中找到的代码
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
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 = …Run Code Online (Sandbox Code Playgroud)