两个Integer变量a和b,值相等,a==b等于多少?

Integer a = Integer.valueOf(127);
Integer b = Integer.valueOf(127);
 
Integer c = Integer.valueOf(128);
Integer d = Integer.valueOf(128);
 
System.out.println(a == b);
System.out.println(c == d);
 
结果:
true
false
 
缘由:
Integer类已经对数字0-127作了一个缓存,建立0-127这些经常使用整数类型是直接从缓存中调引用,因此是true,而超过127则是建立新的对象,因此内存地址的引用不一致,故是false。
相关文章
相关标签/搜索