I have the following code,
Object testA = new Object();
Object testB = testA;
System.out.println("A:"+testA.hashCode())
System.out.println("B:"+testB.hashCode())
Per the above, I get the same hashcode for the two objects. I understand that testB is assigned testA and so it could have the same hashcode, however there should be a way to uniquely identify the difference in both these objects right?
Please let me know if there is something obvious that am missing!
There is no difference, since there are no two objects. There is just one object referred by two variables.
In theory, two different objects may have the same
hashCode
. You can tell them apart by usingequals
or by using==
. If you don't overrideequals
, it behaves as==
by default.