Float f1 = 5.25f;
Float f2 = 5.25f;
然後
f1 == f2
是假的。雖然
f1.equals(f2);
是正確的。爲什麼會這樣,我認爲,如果執行了拆箱,然後F1 F2 ==應該比較
f1.floatValue() == f2.floatValue();
一樣
f1.equals(f2);
應該做的。哪裏不對?
更新:不,我看到答案,因爲Java也比較了Float對象的引用。我問這個問題,因爲我有
Long l = 15l;
Long l2 = 15l;
但
System.out.println(l == l2);
輸出被
true
所以我的誤導,並認爲數字類型的對象是按值使用=相比=。 但我發現,小長值是比較將返回true,因爲small long values are cached!
,因爲它們是對象,'=='比較引用。 – njzk2
@ njzk2聽起來像是對我的回答:) –
沒有理由取消裝箱,因爲沒有任何參數要求進行基本比較 – njzk2