Why equals and hashCode were defined in Object?

2019-01-14 22:40发布

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 logical to make two interfaces:

interface Equalable {
    boolean equals(Equalable other);
}

interface Hashable extends Equalable {
    int hashCode();
}

For example HashSet definition might look like

class HashSet<T extends Hashable> ...

It would prevent one of the common beginner mistakes - using set of items without implementing equals/hashCode.

9条回答
疯言疯语
2楼-- · 2019-01-14 22:44

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.

查看更多
Lonely孤独者°
3楼-- · 2019-01-14 22:45

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.

查看更多
迷人小祖宗
4楼-- · 2019-01-14 22:47

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.

查看更多
一夜七次
5楼-- · 2019-01-14 22:50

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

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-14 22:52

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.

查看更多
我想做一个坏孩纸
7楼-- · 2019-01-14 22:52

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.

查看更多
登录 后发表回答