從回答一個question about primitive types and autoboxing in java:爲什麼在這個例子中的Integer.valueOf(...)比較返回不同的值?
爲biziclop:
類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)); }
}
結果:
C:\Documents and Settings\glow\My Documents>java biziclop false false true false C:\Documents and Settings\glow\My Documents>
這是爲什麼?
我並沒有打算比較這些價值,但正在尋找一個解釋,爲什麼兩個比較的結果是不同的,而他們似乎在做同樣的事情。 – steps 2011-03-08 21:32:31