aspose file tools*
The moose likes Beginning Java and the fly likes comparing contents of 2 objects 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 "comparing contents of 2 objects" Watch "comparing contents of 2 objects" New topic
Author

comparing contents of 2 objects

raj kothari
Greenhorn

Joined: Nov 21, 2005
Posts: 24
I create an object. ( retained as old object )
Update to database as one row of an table in database.
Retrieve the same record from database.
convert it to object. ( referred as new object )
They are two different objects with different reference names belong to a particular class.
I have to check that each fields in both objects are same to ensure that input data were properly updated to database fields?
Which method should I use?
==, equals, or comparable interface..
How do I do it ?
Maurizio Nagni
Ranch Hand

Joined: May 29, 2004
Posts: 75
if you have only to perform an EQUAL/NOT EQUAL operation you can simply override the equal method.
BUT if you have to perform any kind of ordering must use the Comparable; the aim of this interface it is not only to allow you to make an EQUAL but mainly to say "this is bigger than that", or more precisely allow you to order two object according to your idea of "ordering"

ciao
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi,
as you might guess, there is difference in == operator and the
Object.equals() method.
The == operator compares objects references and not object contents, so dont be surprised to see it return true for two entirely different objects.
I would like to suggest you to override the Object.equals() method, the most recommended way to compare the contents of custom defined Objects.

Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
Amit said:
The == operator compares objects references and not object contents, so dont be surprised to see it return true for two entirely different objects.


Since == compares references, how can it return true for entirely different objects? If the references are pointing to two different objects, == will return false.

Did you mean that == will return false when the two references you are comparing point to different objects with the same contents? Many beginners can be confused by this behavior and it's important an point to be clear on.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The original post is talking about fields in the context of a database, so they might want to figure out exactly which fields changed while building SQL. I've had this requirement before and decided to track changes as they happened. The setField() methods also set a "dirty bit" for each field.

To the OP: Any of these guesses sound like what you need?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi,
Thanks Bill for making things more clear.
But i meant to say exactly the same thing, just messed up the words .
I meant that if you have two entirely different objects, and then you perform an assignment of one to another, that is you assign reference.
So thereafter any comparison using the == operator returns true but the fact that the original objects were different can not be neglected.

Hope i have clarified my point
raj kothari
Greenhorn

Joined: Nov 21, 2005
Posts: 24
Originally posted by Stan James:
The original post is talking about fields in the context of a database, so they might want to figure out exactly which fields changed while building SQL. I've had this requirement before and decided to track changes as they happened. The setField() methods also set a "dirty bit" for each field.

To the OP: Any of these guesses sound like what you need?
raj kothari
Greenhorn

Joined: Nov 21, 2005
Posts: 24
I need to check from the database context whether all my fields were updated properly. If I override the equals method, do I need to manually check all the instance variables of ob1, obj2 something like obj1.age==obj2.age, and so on different checking for strings, int, boolean instance variables?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
Originally posted by raj kothari:
I need to check from the database context whether all my fields were updated properly. If I override the equals method, do I need to manually check all the instance variables of ob1, obj2 something like obj1.age==obj2.age, and so on different checking for strings, int, boolean instance variables?


Yes


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing contents of 2 objects