【发布时间】:2014-10-28 19:48:01
【问题描述】:
System.out.println(Integer.valueOf(5) == Integer.valueOf(5));
System.out.println(Integer.valueOf(500) == Integer.valueOf(500));
输出是
true
false
为什么第一行返回true,第二行返回false?这里有什么诀窍,因为两者都在 Integer 类上调用 valueOf()。
【问题讨论】:
-
这是比较指针而不是整数。事实上,对于 5,它返回相同的指针只是一个优化,一个实现细节。 (仅供参考,我根本不懂 Java,我只是 read the documentation...)