• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Object comparison

 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Arijit!!
 
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'

reply
    Bookmark Topic Watch Topic
  • New Topic