java为什么需要整数。价值(…)本例中的比较返回不同的值?
从a question about primitive types and autoboxing in java的答案来看:
for biziclop:
class biziclop {
public static void main(String[] args) { System.out.println(new Integer(5) == new Integer(5)); System.out.println(new Integer(500) == new Integer(500)); System.out.println(Integer.valueOf(5) == Integer.valueOf(5)); System.out.println(Integer.valueOf(500) == Integer.valueOf(500)); }
}
Results in:
C:\Documents and Settings\glow\My Documents>java biziclop false false true false C:\Documents and Settings\glow\My Documents>
为什么
# 1 楼答案
见整数。实现值:http://docjar.com/html/api/java/lang/Integer.java.html(850s线)
# 2 楼答案
整数。valueof根据Java语言规范的要求,将对象缓存为零左右的值
受ilya's answer启发,参见即将出版的JDK7第638-643行中的the latest, actual source for Integer.valueOf()