Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object comparison

 
Arijit Ghosh
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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 ?
 
Don Bosco
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right Arijit!!
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic