0

I am studying Overriding hashCode() and equals(Object obj) methods of Object class.

body of equals(Object obj) method in Object class is :

public boolean equals(Object obj) {
    return (this == obj);
}

and hashCode() is native :

public native int hashCode();

I have a class Test with overrided equals(Object obj) and hashCoe() :

public class Test {

public static void main(String[] args){
    Test t1 = new Test();
    Test t2 = new Test();

    System.out.println("t1 toString() : " + t1.toString());
    System.out.println("t1, Hex value of hashcode : " + Integer.toHexString(t1.hashCode()));
    System.out.println("t2 toString() : " + t2.toString());
    System.out.println("t2, Hex value of hashcode : " + Integer.toHexString(t2.hashCode()));
    System.out.println(t1.equals(t2));
}

@Override
public int hashCode() {
    return 999; //hard coded value is just for testing
}

@Override
public boolean equals(Object obj) {
    return (this == obj);
}
}

Output of my Code is :

t1 toString() : demo.Test@3e7
t1, Hex value of hashcode : 3e7
t2 toString() : demo.Test@3e7
t2, Hex value of hashcode : 3e7
false  //why it is false

why equals(Object obj) returns false in this case if both objects toString() returns the same reference ID (hashcode) [I am not sure if it compares hashcode or not].

What does == operator actually compare in case of objects?

in this answer, answerer said that == that is, it returns true if and only if both variables refer to the same object, if their references are one and the same.

How does it know that the variables refer to the same object???

Community
  • 1
  • 1
user3603328
  • 38
  • 1
  • 5
  • 3
    Cue the avalanche of answers/close votes/downvotes/the inevitable amazing Jon Skeet answer. *Grabs popcorn bucket* – JamesENL Jul 15 '14 at 07:59
  • Sure, there's an answer somewhere in SO, but the answer is just simple as possible: `==` checks references to be equal: these must be the same objects. – Dmitry Ginzburg Jul 15 '14 at 08:01
  • A hashcode is just that: a hashcode, not a reference id, not a memory location; it doesn't describe the object identity. It is intended to simplify looking up an object in - for example - a `HashMap`. It is more related to `equals` than to `==` – Mark Rotteveel Jul 15 '14 at 08:02
  • 1
    possible duplicate of [Java == vs equals() confusion](http://stackoverflow.com/questions/7520432/java-vs-equals-confusion) – Dmitry Ginzburg Jul 15 '14 at 08:02
  • @DmitryGinzburg That is the question linked in the question itself; this question asks an additional question so it is not a duplicate of that question. – Mark Rotteveel Jul 15 '14 at 08:03
  • 1
    Whoever answers this question instead of voting to close it - shame on you. – Jan Zyka Jul 15 '14 at 08:05
  • @JanZyka I disagree, the question "How does it know that the variables refer to the same object?" is interesting as demonstrated by the [answer by Jon Skeet](http://stackoverflow.com/a/24752897/466862) – Mark Rotteveel Jul 15 '14 at 08:08
  • I belive the same has been told here so many times before that it's not worth answering yet again, just my opinion though. – Jan Zyka Jul 15 '14 at 08:13

4 Answers4

6

How does it know that the variables refer to the same object?

Because the values of the variables are the same references. (Your variables are t1 and t2. The values of those variables are references. Those references are used as a way of navigating to objects, basically.)

Suppose you and I both have pieces of paper with a house's street address on (that's my usual analogy for "variables with references"). How do we check whether they refer to the same house? We see whether the address is the same.

There are some potential twists here, as in some cases the form of the reference may not be the same between two expressions, but that's the basic idea.

(Note that just because I've used "address" in the analogy, that doesn't mean a Java reference is always a memory address. It's "a way of navigating to an object", that's all. It may or may not just be an address.)

From the JVM specification section 2.2:

The Java Virtual Machine contains explicit support for objects. An object is either a dynamically allocated class instance or an array. A reference to an object is considered to have Java Virtual Machine type reference. Values of type reference can be thought of as pointers to objects. More than one reference to an object may exist. Objects are always operated on, passed, and tested via values of type reference.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

== will check if both references are the same. You have 2 different objects, no matter they are equivalent, they point to different memory blocks.

The only exception to this rule is String, in special conditions(i.e. invoking .intern() method), but that's really a special case, related to String pool.

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
0

If you compare with == equals, the instances of the Object needs to be the same (pointer to the same reference, "same id" in the JVM). The hashcode of the object is'nt checked.

This is why it is a good practice to compare with equals(..) in Java.

Here some code:

Test o1 = new Test();
Test o2 = new Test();

//You can check the ids with System#identityHashCode(Object):
System.out.println(System.identityHashCode(o1));
System.out.println(System.identityHashCode(o2));

o1 == o1 // will be true
o1 == o2 // will be false

o1.equals(o2) //depends on how you have implemented equals() and hashCode() in the Test Object.

The contract between hashCode and Equals is:

objects which are .equals() must have the same .hashCode()

The reverse statement does not need to be true.

In your example, it is exacly the case: you return 999 in .hashCode() and you compare the jvm ids in .equals().

Jmini
  • 9,189
  • 2
  • 55
  • 77
  • 1
    There can still be duplicate identity hash codes even if objects are not `==`, and `equals` does not depend on `hashCode` (although they do need to be consistent) – Mark Rotteveel Jul 15 '14 at 08:09
-1

== checks to see if the two objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location

EX1:

String obj1 = new String("xyz");

String obj2 = new String("xyz");

obj1==obj2; //False

EX2

String obj1 = new String("xyz");

String obj2 = obj1;

obj1==obj2; //True

kranthi
  • 11
  • 1