类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) 我有一个代码片段:
public class Test{
public static void main(String args[]){
Integer a = 100;
Integer b = 100;
Integer c = 5000;
Integer d = 5000;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
if(a == b)
System.out.println("a & b Both are Equal");
else
System.out.println("a & b are Not Equal");
if(c == d)
System.out.println("c & d Both are Equal");
else
System.out.println("c & d are Not Equal");
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么输出是这样的?的Output是:
a & b Both are equal
c & d are not equal
我使用jdk1.7