difference between equals() and hashCode()

前端 未结 7 589
心在旅途
心在旅途 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:17

    1. As other said '==' compares references. But the two methods are just methods doing something which can be overridden.
    2. Every method does something. If you want to know what it exactly does and what is its meaning you need to read the documentation.
    3. You may override those methods in anyway you want. But please note that you must follow JAVA documentation for these two methods. Because they are used by other classes. For example equals() is used while you try find an object in a list and .hashCode() is used in some hashtable classes provided by JAVA class library.

提交回复
热议问题