2016-03-02 116 views

回答

0

與原始int相反,整數是一個對象。您的比較是比較Integer類型的兩個對象。我有點驚訝,你會得到「錯誤的真實」。如果你想嘗試:

System.out.println(i1.intValue() == i2.intValue()); 
System.out.println(i3.intValue() == i4.intValue()); 

你應該得到預期的結果。

+0

你也應該閱讀:http://stackoverflow.com/questions/1700081/why-does-128-128-return-false-but-127-127-return-true-when-converting-to-整合 – Eashi

+0

這不回答這個問題。 – erickson

2

Integer實例的一個值的範圍(至少-128 – 127)的高速緩存,當隱式轉換的intInteger時使用。

在這種情況下,128不在緩存中,因此代表該值的每個Integer對象都是新的且不同的。

另一方面,值127,保證在緩存中,因此重複獲得Integer的同一個實例。

相關問題