Why equals and hashCode were defined in Object?

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

    The default implementation in java.lang.Object makes sense. Often times, it's good enough. In JPA/web applications, I find myself very rarely if ever overriding equals and hashCode.

    A better question might be: for immutable value objects like String, Long etc., why can't you override the == operator to call equals(), like you can in C#? I've seen far more errors because of that than I have of the default equals/hashCode not doing the right thing. For example,

    Long x = obj.getId(); 
    Long y = obj2.getId();  
    if (x == y) { // oops, probably meant x.equals(y)! }
    

    It's a fair question, though, why the default methods aren't locked behind a tagging interface like the default Object.clone(). There's a default implementation but you have to explicitly acknowledge that you want to use it by implementing Cloneable. There could just as easily have been a similar tagging interface like Collectible or Equatable, and then the signature for collections methods could have been Equatable instead of Object.

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

    Mhh not sure but when Java 1.0 was released generics did not exist yet. They were added in Java 5.0 in 2004.. so your proposal could not be implemented for Java 1.0

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

    (Personally, if they were in an interface, I'd put them both in there to avoid at least one class of equals/hashCode mistakes.)

    I think you'd want an implementation in Object, as a fall-back mechanism, meaning everything would have an implementation anyway, interface or not.

    I suspect a lot of it is historical; programming Java today looks quite a bit different than programming Java back then did.

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

    When we implement an interface we inject (or accept) the contract defined by the interface.

    Equalable & Hashable are two different contracts. But if we take a look closely then we will see that both of them depend on each other, which means they are part of a single interface, something like EqualableAndHashable.

    Now the obvious question is, whether they should be part of this new EqualableAndHashable interface or Object?

    Let's find out. We have == (equal operator) to check equality of two objects. == operator confirms whether values/references are equal for two different primitives/objects. But this is not always possible to answer just by checking with the == operator.

    Now question is, whether this equality, which is also a contract, should be injected via interfaces or part of the Object class?

    If we take a look, we can't just say something like:

    TypeX does not guarantee the equality contract.

    It will become a chaos if some object types offer equality and some do not. Which means object of TypeX must honor the equality contract which is true for all other object types as well. So, it must not inject equality from a interface, because equality should be the part of the contract for any object by default, otherwise it will create chaos.

    So we need Objects to come up with implementation of equals. But it can't implement only the equals method, it also needs to implement the hashcode method.

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