I would like to print the "object reference" of an object in Java for debugging purposes. I.e. to make sure that the object is the same (or different) depending on the situation.
The problem is that the class in question inherits from another class, which has overriden both toString() and hashCode() which would usually give me the id.
Example situation: Running a multi-threaded application, where I (during development) want to check if all the threads use the same instance of a resource object or not.
What exactly are you planning on doing with it (what you want to do makes a difference with what you will need to call).
hashCode
, as defined in the JavaDocs, says:
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)
So if you are using hashCode()
to find out if it is a unique object in memory that isn't a good way to do it.
System.identityHashCode
does the following:
Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode(). The hash code for the null reference is zero.
Which, for what you are doing, sounds like what you want... but what you want to do might not be safe depending on how the library is implemented.