参见英文答案 > Weird Integer boxing in Java 10个
我有一个代码片段:
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");
}
}
最佳答案
这是由于虚拟机中的优化将小(常用)整数映射到重用的对象池. This answer解释了一些细节.
原文链接:https://www.f2er.com/java/438351.html