-3

I have been stuck with this for a while now, Actually I have 2 objects which according to me are same, I have overriden the hashCode method to create equal hash codes for both, still when I compare them for equality using "==" or Object's equals(which too uses "=="), it returns false to me, The below scenario should exactly explain:::

1)HashCode-->-626561382 AND 2)HashCode--->-626561382  
1)IdentityHashCode-->19640463
2)IdentityHashCode-->22330755  
1)Bean1=beans.OrdersBean@daa76e9a  AND 2)Bean2=beans.OrdersBean@daa76e9a  
Check MySelf for(==)-->false  
Check Object's Equals()-->false

Please kindly explain me why is this happening????

davioooh
  • 23,742
  • 39
  • 159
  • 250
KDjava
  • 2,355
  • 4
  • 17
  • 22
  • 1
    http://stackoverflow.com/questions/7520432/java-vs-equals-confusion – billc.cn Jul 27 '12 at 12:58
  • But , whenever a new object is created , the hashCode() method is called, and as I have over-ridden the hashCode() method my hashCode should be run(exactly this is happening) and as we know that the hashCode() is actually the representation on how and where on the heap the Object will be created, this means when hashCode is same for 2 objects then they should be pointing to the same instance(first one) on the heap, Thus, when "==" compares the memory references then this should return true.. Correct me if wrong..!! – KDjava Jul 27 '12 at 13:20
  • 1
    You are wrong. The hashcode has nothing to do with the == operator. == is checking for reference equality, the hashCode() method is cheking the hashCode of the instance (however you have decided it should be calculated), the fact your hashcodes are the same just means that whatever your hashCode method is doing is calculating the same hasCode for both objects, which is what it should do if the objects equals method returns true. And the equals method should return true in any situation the programmer decides the two objects are equal in value. – Jon Taylor Jul 27 '12 at 13:41
  • @JonTaylor As per JLS7,for hashCode; "This is typically implemented by converting the internal address of the object into an integer...."; Thus if hashCodes are equal for 2 objects that means that their internal address within the memory are equal, and "==" compares that only , that is if the objects have the same address within the memory, :: Correct me if wrong..!! – KDjava Jul 28 '12 at 09:57
  • 1
    You may be correct in terms of the in built implementation although I don;t think this should ever be relied upon since it can be overridden and as you stated in your previous comment "and as I have over-ridden the hashCode() method" you have indeed overridden the hascode method, so it no longer has its default behaviour anyway. – Jon Taylor Jul 28 '12 at 15:04
  • @JonTaylor Absolutely Jon, I couldn't agree with this more, that actually was my question, that if I over-ride hashCode() in that case the objects that are already created in the memory will actually still be having the same memory addresses on the heap as their initial one, This means that hashCode() is just present to define the address of the hash bucket where the references(handlers) to the actual objects will be put in(in case of hash -based collections), but still after reaching the hash bucket , to eventually get to the actual object you'll always have to use the equals() method. – KDjava Jul 29 '12 at 16:26

5 Answers5

11

== operator compare refrences(Memory location) of Objects in java...

if you compare objects then use .equals()

if(obj1.equals(obj2)){

}
Community
  • 1
  • 1
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
4

To compare two objects for equal value you need to override the equals method.

The == operator as others have mentioned compares references (i.e. is it the exact same object)

Explanation

If you take the example of identical twins Ben and Adam, using an == would return false when comparing the two since Ben is not Adam (even though they look the same), if you use .equals and the comparison is based on looks then this would return true.

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
3

In Java == is used to compare reference. To valuate if two objects are equivalent use equals.

Note If you have to compare custom objects consider ovverriding equals in your class according to your equivalence criteria.

davioooh
  • 23,742
  • 39
  • 159
  • 250
1

Override .equals from the parent Object's method, this is intended for "deeper" comparisons whereas == pertains to checking that the references (identifiers) are referring to (so that updates to one apply to both) the same instantiation.

John
  • 6,433
  • 7
  • 47
  • 82
0

1. Using '==' :

When you want to check if two reference variables are referring to a same object you should use == operator in java. For example - (Assume there is a class called Person)

Person person1 = new Person();
Person person2 = person1;

System.out.println(person1 == person2); // true

Here as we have used new only once, only one object is getting created in the heap memory and we are assigning it to a reference variable -> person1. In the second statement we are assigning person1 to person2. So actually there is only one object in memory but both person1 and person2 are referring to the same object.

[In short we can say that, similar to primitives, == compares the value in the variable which in case of reference variables is memory address of the actual object].

2. Using '.equals()' :

When you want to check if the two objects are meaningfully equal then use .equals() method. For example -

Person person1 = new Person();
Person person2 = new Person();

System.out.println(person1.equals(person2)); // false

Here we are creating altogether two different objects, so they are not meaningfully equal. Hence the equals() method will return false.

Rishikesh Dhokare
  • 3,559
  • 23
  • 34