It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object comparison" Watch "Object comparison" New topic

Object comparison

Arijit Ghosh
Ranch Hand

Joined: Feb 01, 2002
Posts: 174

Output will be False. In my understanding, whenever there any 2 objects being compared, it is actually the 2 references being compared.
However, I am confused about the following fact--

implies that they are 2 different references. Does this mean that they are pointing to different objects ?? I gguess, YES! But does this mean that the object "47", is stored twice at 2 different locations ??

In this case, there is only a single object and 2 references pointing to the same Object. So the OUTPUT is TRUE.
Does this not mean that it actually compares the "MEMORY LOCATION of objects that are being pointed to by n1 and n2" ??
So can I conclude that the value of the Objects are immaterial. If their memory locations are different, it will return FALSE otherwide it will return TRUE.
Am I right ?

Regards,<br /> Arijit
Don Bosco
Ranch Hand

Joined: Oct 31, 2002
Posts: 108
You are right Arijit!!

SCJP 1.4<p>Wingardium Leviosa!!
Bert Bates

Joined: Oct 14, 2002
Posts: 8898
Another way to look at it is :
when comparing reference variables == determines if two reference variables are referring to the SAME OBJECT.
If you are interested in comparing two object's values, use .equals() which is said to be used for 'meaningful equivalency'

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
I agree. Here's the link:
subject: Object comparison
It's not a secret anymore!