在你的問題和缺乏連貫性這麼多混淆。你寫:
我們可以通過在各自的類
沒有覆蓋了equals/hashCode方法比較兩個對象,你不知道。你比較,當你正在執行類似,例如兩個對象:
String firstString = "asdf";
int result = firstString.compareTo("asdff");
你「比較」當你調用實現Comparable
(firstString
)的類的對象上compareTo
方法,將另一個對象("asdff"
)作爲該方法的實際參數。就是這樣,它只是名稱的問題(類型的名稱 - 類和方法的名稱)。
關於等號運算符(==)和等號方法之間區別的問題,這又只是一個定義問題。規則在這裏定義https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.21
即使我們發現兩個對象具有相同的所有屬性值,這是否意味着除此之外的任何內容?
什麼物體?這些對象是什麼類型?他們的屬性是什麼類型?這完全取決於他們如何實施。這個問題非常模糊,但我會說,答案是無論如何。在Java程序(或任何其他語言)的背景下,平等並不意味着超出語言規範和代碼定義的任何內容。
我想他們也會擁有兩個不同的內存位置,不是嗎?
實例(如果你不明白的「斷言」的東西,檢查的JUnit出它只是意味着「這是真/假」。):
@Test
public void testCrazy()
{
Object firstObject = new Object();
// now I have a reference on the stack (firstObject) that points to some memory
// location on the heap that holds some bytes representing an Object instance
Object secondObject = new Object();
// now I have another reference pointing to a DIFFERENT location on the
// heap
boolean equalReferences = (firstObject == secondObject);
// equalReferences is expected to be false
assertFalse(equalReferences);
// look, I make the first reference point to the locations held by the
// second one (tha is what = does if applied to references)
firstObject = secondObject;
// now the 2 references are expected to pass the equality operator test
assertTrue(firstObject == secondObject);
// what if I override equals in a completely nonsense way?
firstObject = new Object(){
@Override
public boolean equals(Object obj)
{
// no matter what, it always returns false.
// does it make sense? Probably not in our common sense, but the
// compiler won't complain
return false;
}
};
secondObject = firstObject;
// now they do point to same same memory location, in fact
assertTrue(secondObject == firstObject);
// but if I call the overriden equals method:
assertFalse(firstObject.equals(secondObject));
// it is still failing even if I call equals on firstObject itself:
assertFalse(firstObject.equals(firstObject));
}
您可以定義平等任何方式你想要的就是這個意思。 – Nozdrum
[Java中==和equals()有什麼不同?](http://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java ) – user140547
如果用「意思是任何東西」,你的意思是「意味着它們是同一個對象」,那麼沒有。如果是「有意義的」,那麼你的意思是「有意義的」,那麼是的。它意味着你定義它的意思。你需要提出更精確的問題。 – user2357112