java的Integer缓冲
java.lang.Integer.valueOf(int)方法默认情况下如果参数在-128到127之间,则返回缓存中的对象,否则返回new Integer(int)。
缓存中的对象是JVM第一次使用valueOf方法时初始化的。
可以设置系统属性 java.lang.Integer.IntegerCache.high 修改缓冲区上限,默认为127。参数内容应为大于127的十进制数形式的字符串,否则将被忽略。取值范围为127-Long.MAX_VALUE,但是用时将强转为int。
当系统中大量使用Integer时,增大缓存上限可以节省小量内存。
缓冲区上限将影响某些业务:
Integer i1 = 127;
Integer i2 = 127;
if (i1 == i2)
System.out.println(true);
else
System.out.println(false);
i1 = 128;
i2 = 128;
if (i1 == i2)
System.out.println(true);
else
System.out.println(false);
以上代码默认情况下将输出:
true
false
当修改缓冲上限大于128时将变为:
true
true