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

Object identity

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw this comment inside the Shutdown class of java.lang :

"Wrapper class for registered hooks, to ensure that hook identity is
object identity rather than .equals identity"

If I am right the .equals() of Object checks if the references of two objects are the same i.e. whether both point to same object in memory.

Doesn't Object identity mean the same thing?

What does the comment imply?
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tests wether both point to the same object. (Object identity)
e.g. string1 = string2 = "value";


tests whether two seperate objects contain the same value
e.g. string1 = new String("value"); string2 = new String("value");
[ October 18, 2006: Message edited by: Scheepers de Bruin ]
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With identity i mean the identity of the object with respect to heap and that can be easily found using the hashcode.In case the hashcode of the considered object is overriden then you can go for identityHashCode method in the System class to get the actual identity of the ocject.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rahul, I don't you why you suddenly start talking about hash codes, but you cannot use Object.hashCode() or System.identityHashCode(...) to check if two objects are equal.

Sure, if two objects are equal then they will have the same hash code, but two different objects may also have the same hash code. Hash codes only work "in one direction", not in both directions. The hash code of an object does not represent the unique identity of an object.

Read the API documentation of Object.hashCode() for information on how hash codes work in Java.
[ October 18, 2006: Message edited by: Jesper Young ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:
Rahul, I don't you why you suddenly start talking about hash codes, but you cannot use Object.hashCode() or System.identityHashCode(...) to check if two objects are equal.

Sure, if two objects are equal then they will have the same hash code, but two different objects may also have the same hash code. Hash codes only work "in one direction", not in both directions. The hash code of an object does not represent the unique identity of an object.

Read the API documentation of Object.hashCode() for information on how hash codes work in Java.

[ October 18, 2006: Message edited by: Jesper Young ]



Its true like what you have explained about equals and hashcode of an object.I was trying to explain the literal meaning of identity of an object with respect ot its location in the heap.
That is all.
Thanks,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic