• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to check the two objects are same?

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

How to check the two objects are same?

Regards
Ganesan
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use the equals() method.
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic