aspose file tools*
The moose likes Beginning Java and the fly likes How to check the two objects are same? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to check the two objects are same?" Watch "How to check the two objects are same?" New topic
Author

How to check the two objects are same?

Ganesan Ramakrishnan
Ranch Hand

Joined: Mar 18, 2008
Posts: 89
hi all,

How to check the two objects are same?

Regards
Ganesan
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

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

Joined: May 31, 2007
Posts: 3223
use the equals() method.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

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

Joined: Apr 29, 2008
Posts: 234
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

Joined: Apr 29, 2008
Posts: 234
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

Joined: Aug 05, 2005
Posts: 3646
    
  15
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.


Joanne
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to check the two objects are same?