Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing contents of 2 objects

 
raj kothari
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic