Why equals and hashCode were defined in Object?

前端 未结 10 1179
天命终不由人
天命终不由人 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 05:59

    Originally, in Java, there were no generics. This was worked around by allowing any Object to be a member of any collection, and thus any Object needed hashCode and equals. By now it's too entrenched to change.

    0 讨论(0)
  • 2020-12-06 05:59

    Any object, regardless of its type, can sensibly answer whether it's equivalent to any other object, even if the other object's type is one it's never heard of. If it's never heard of the other object's type, that fact alone is sufficient for it to report that it isn't equivalent to the latter object.

    0 讨论(0)
  • 2020-12-06 06:00

    Add a note to @Kowser's answer about 'chaos':

    The implicit logic of equals requires that it must meet the following characteristics:

    • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
    • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
    • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
    • ...

    Obviously, if the equals method is not defined in the Object, it will break 'symmetric' and 'transitive', which is equivalent to breaking the intrinsic logic of equals.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-06 06:03

    Its a generic implementation. You are supposed to override the implementation if you need them. Otherwise you have a reasonable default implementation.

    At least the equals is a must have. Creating interfaces for basic operations probably would involve to much overhead.

    0 讨论(0)
  • 2020-12-06 06:04

    Really, it's just for convenience, and it's better this way. Well, think about what it would take to do object equality if you didn't have the .equals method:

    AreEqual(Object obj1,Object obj2) {
      if(!obj1 instanceof Equalable) return false;
      if(!obj2 instanceof Equalable) return false;
      return ((Equalable)(obj1).equals((Equalable)obj2);
    }
    

    This is true even for a hashCode. Sometimes reference equality is enough. If you made HashSet only accept objects that implement Hashable, then you'd have to explicitly make your classes Hashable, even though you only want reference equality. And you've reduced the universality of an otherwise great data structure.

    It's better to have Object have a default (and sometimes sufficient) .equals and .hashCode function and cause a few problems for new comers, than to make frequent users of the language trudge through more red tape.

    0 讨论(0)
提交回复
热议问题