File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Object identity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Object identity" Watch "Object identity" New topic
Author

Object identity

Nikhil Sun
Ranch Hand

Joined: Nov 13, 2005
Posts: 108
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

Joined: Jul 19, 2005
Posts: 99

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 ]

We're doomed!!<br />Yay!!!<br />No that's bad Girr!!<br />Yay!!!
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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.


Rahul Bhattacharjee
LinkedIn - Blog
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
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,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object identity
 
Similar Threads
hibernate entity a.equals(b) but a != b
equals on array
java.lang.UnsupportedOperationException: Result lists are read-only.
types in equals()???
hashCode() and System.IdentityHashCode