Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object identity

 
Nikhil Sun
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • 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?
 
Scheepers de Bruin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • 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 ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15288
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic