Why equals and hashCode were defined in Object?

前端 未结 10 1198
天命终不由人
天命终不由人 2020-12-06 05:50

What\'s the reasoning behind decision to include these methods in the java.lang.Object? Equality and hashing doesn\'t make sense for many classes.

It would be more l

10条回答
  •  南方客
    南方客 (楼主)
    2020-12-06 06:02

    If you have a list of objects and you call the contains method what should Java do? I think the default implementation(comparing references) is a decent decision. This way you won't have to implement yourself equals and hashcode for every class you use in a collection.

提交回复
热议问题