difference between equals() and hashCode()

前端 未结 7 588
心在旅途
心在旅途 2021-02-05 13:29

I want a brief definition about the equals() , \"==\" and hashCode(). If i run following code means the output will be \"true false 2420395 2420395\". But i had understand that

7条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-05 14:21

    equals() and hashCode() are different methods and hashCode method should not be used to check if two object references are same. Reason: hashCode just returns int value for an Object, even two different objects can have same hashCode integer. The value returned by hashCode() is the object's hash code, which is the object's memory address in hexadecimal. equals() checks if the two object references are same. If two objects are equal then their hashCode must be the same, but the reverse is not true.

提交回复
热议问题