What does equals(Object obj) do?

后端 未结 7 843
天涯浪人
天涯浪人 2020-12-02 21:23

I´ve often found an equals method in different places. What does it actually do? Is it important that we have to have this in every class?

   public boolean          


        
7条回答
  •  长情又很酷
    2020-12-02 22:14

    It enables you to re-define which Objects are equal and which not, for example you may define that two Person objects as equal if the Person.ID is the same or if the Weight is equal depending on the logic in your application.

    See this: Overriding the java equals() method quirk

提交回复
热议问题