标签:cache int 知识 high static 128 Integer
public static void main(String[] args){
Integer i1 = 100;
Integer i2 = 100;
System.out.println(i1 == i2);
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i3 == i4);
}
答案:
true
false
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) -1);
}
cache = new Integer[(high - low) + 1];
int j = low;
for(int k= 0; k< cache.length; k++)
cache[k] = new Integer(i++);
}
private IntegerCache( {}
}
在Interger类中,存在一个静态内部类lntegerCache,该类中存在一个Integer cache[],并且存在一个static块,会在加载类的时候执行,会将-128至127这些数字提前生成Integer对象,并缓存在cache数组中,当我们在定义Integer数字时,会调用Integer的valueOf方法,valueOf方法会判断所定义的数字是否在-128至127之间,如果存在则直接从cache数组中获取Integer对象,如果超过,则生成一个新的Integer对象。
标签:cache,int,知识,high,static,128,Integer 来源: https://blog.csdn.net/zrllllll/article/details/120233480
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。