• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How can we compare two objects ?

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can we compare two objects ?
Is it ok
if(obj1 == obj2)




 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably a beginner question, but here goes:

(obj1 == obj2) returns true if and only if obj1 is the same object as obj2.

Think of two cars -- myCar and yourCar. (myCar == yourCar) is true if there is actually only one physical car there. It doesn't matter if we both drive identical puke green 1972 Oldsmobiles, they are still not the same car, so they won't be ==.

There is a method called equals() defined in the Object class that should be overridden for cases in which you want to see if the two cars are the same, for instance if you want to determine the value of a car, they would be equal (if we leave out a lot of details) even if they don't have the same VIN or color.

So, the short answer is, you can compare objects with equals() and references to objects with ==
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "==" operator just checks if the Object referenced by the 2 reference variables is the same

for eg.

Object a = new Object();
Object b = a;
a==b evaluates true

However it does not compare the contents of the Object

Incase you want to do that you will have to implement the comparison by overloading the equals method and using it to compare the Objects.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just my 2 cents:

How can we compare two objects ?


(obj1 == obj2) returns true if and only if obj1 is the same object as obj2.

Think of two cars -- myCar and yourCar. (myCar == yourCar) is true if there is actually only one physical car there. It doesn't matter if we both drive identical puke green 1972 Oldsmobiles, they are still not the same car, so they won't be ==.


== is an operator
it compares two object references
so, when you say
a == b,
you'r checking if those two references point to the same object (which is in memory)

now, if you want to compate two objects, you'v to use equals() and hashcode()

this will help you
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it compares two object references
so, when you say
a == b,
you'r checking if those two references point to the same object (which is in memory)



I think you'll find that's what I said in the last sentence.

 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my sole intent was to clarify
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, sorry.

I know my style leaves a lot to be desired. I always think "if I can just put it in a real world context, everyone will understand". I use too many long, drawn out examples because it fits my learning style. I'll try to make it more concise.
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic