• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check the two objects are same?

 
Ganesan Ramakrishnan
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

How to check the two objects are same?

Regards
Ganesan
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How to check the two objects are same?

In order to check two objects ( I hope you are talking about different objects) are different you have to override the equal method of Object class.
 
arulk pillai
Author
Ranch Hand
Posts: 3388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use the equals() method.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have this code:


you check if the object references points at the same place in memory by using ==.

one == two gives you false.
one == copy gives you true;

However, if you want to compare the objects, you have to use the method equals(Object other).

So if you do this:
one.equals(two) you get false.
one.equals(copy) also gives you false...

Why??

That is since the equals-method is in the class Object and that only checks by reference.

You have to override the equals method in your own classes, like this.



Then it would be right.
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since Ganesan is talking about two objects. So i think he is talking about two different objects.

Two different objects can not same if we dont override the equal method.

Without overriding it we always get false for two different objects.
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ove

one.equals(copy) also gives you false...


I think one.equals(copy) will rturn true if equals method is not overriden because these are the 2 references to the same object
and since we are not overriding equals method. Therefore equals method will work the same way as "==".

Please correct me if i am wrong.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bittoo garg:
Hi Ove


I think one.equals(copy) will rturn true if equals method is not overriden because these are the 2 references to the same object
and since we are not overriding equals method. Therefore equals method will work the same way as "==".

Please correct me if i am wrong.


You're almost right.
Actually, one.equals(copy) will return true whether the equals method is overridden or not. Both references point to the same object, so they are going to be equal however you compare them.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bittoo garg:
Hi Ove


I think one.equals(copy) will rturn true if equals method is not overriden because these are the 2 references to the same object
and since we are not overriding equals method. Therefore equals method will work the same way as "==".

Please correct me if i am wrong.


No, you are totaly right. Made a copy-paste-error. Thanks for pointing this out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic