AAA
AAA

Reputation: 11

Same object different hash code?

class Rational {
    int num = 0;
    int denom = 0;
    public Rational(int num, int denom) {
        this.num = num;
        this.denom = denom;
    }

public static void main(String[] args) {
    Rational r1 = s.new Rational(1, 1);
    Rational r2 = s.new Rational(1, 1);
    System.out.println(r1.hashCode());
    System.out.println(r2.hashCode());

}

I have two of the same objects, but they have different hashCode. Why is that? I tried overriding the .equal method in Rational so r1.equals(r2) == true. But they still produce different Java hashCode.

Upvotes: 0

Views: 8769

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1074276

I have two of the same objects

"Same" usually means, literally, one object. Here what you have are equivalent objects.

...but they have different hashCode. Why is that?

Because you haven't overridden hashCode to replace the default implementation. Overriding equals doesn't change hashCode (you almost always have to override both if you override equals).

The hashCode documentation says this about the default implementation in Object:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)

So you'll get distinct integers (usually) for different instances, even if they're equivalent, unless you replace hashCode.

In your case, you could do this:

@override
public int hashCode() {
    return this.num ^ this.denom;
}

...and have a reasonable hashCode for Rational (be sure to override equals as well), but there are lots of other ways. Just stick to the contract:

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

Upvotes: 3

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

They are not the same object; they are two different objects with the same value. Just because there's another Jeroen Vannevel in the world doesn't mean it's me.

hashCode() is not linked to equals(): they both have a contract they have to adhere to and they are related but they do not directly influence eachother. That's why you should always override both of these methods and not just one.

Upvotes: 5

Related Questions