Object Equality

K

Karl Gorden

I have two objects in seperate areas of my code, that are supposed to come
from an object pool. I think there is a bug somewhere and two copies of
these objects are appearing. I've tried my best to track it down but I can't
seem to isolate this.

What I want to do is on each access of these objects, print out some
information about the object so I know for certain if I've got two copies.
So is there a way to output the object memory address? If they are different
then I know I have two copies. I know toString() tends to print out the
memory address but my objects subclass another object that overrides this
and marks it final.

I don't have the objects together so I can't do obj1 == obj2.

Any help ?
 
A

Anton Spaans

If you could have used 'toString()', if it wasn't overridden, but you can't
because it *is* overridden, use the 'hashCode()' method instead.

The Object.toString() implementation basically returns the class name plus
the hash-code of the object.

To be even more sure the objects are equal, for each object, get the
class-name (getClass().getName()) and append this with the hashCode(). The
hashCode() method must guarantee that two equal objects return the same
value for hashCode() (however, the other way is not guaranteed: If two
objects return the same hash-code, they are not necessarily equal).

However, i don't understand your sentence "I don't have the objects
together".... If you don't have access to the objects (objects' references),
how would you ever be able to check for their equality?

-- Anton.
 
M

Michael Borgwardt

Anton said:
If you could have used 'toString()', if it wasn't overridden, but you can't
because it *is* overridden, use the 'hashCode()' method instead.

Which could also be overridden.
To be even more sure the objects are equal, for each object, get the
class-name (getClass().getName()) and append this with the hashCode(). The
hashCode() method must guarantee that two equal objects return the same
value for hashCode()

And therein lies the rub: what if the objects are in fact equal (equals()
method is overriden), but not identical? It seems to me that Karl has
exactly that case and still wants to tell them apart!

That's what System.identityHashCode() is for.


To be exact: Object.toString(), which is based on Object.hashCode(), prints
out some value that is different for different objects "As much as is
reasonably practical". And it is not the memory address itself but "typically
implemented by converting the internal address of the object into an integer,
but this implementation technique is not required"
 
K

Karl Gorden

Michael Borgwardt said:
Which could also be overridden.


And therein lies the rub: what if the objects are in fact equal (equals()
method is overriden), but not identical? It seems to me that Karl has
exactly that case and still wants to tell them apart!

That's what System.identityHashCode() is for.

Thank you Michael.
 
A

Anton Spaans

Hi!

I read this from the API docs:

public static int identityHashCode(Object x)Returns the same hashcode for
the given object as would be returned by the default method hashCode(),
whether or not the given object's class overrides hashCode(). The hashcode
for the null reference is zero.

Parameters:
x - object for which the hashCode is to be calculated
Returns:
the hashCode
Since:
JDK1.1

It seems that the identityHashCode() is no better than the non-overridden
hashCode()... Still, if two objects 'x' are different the
identityHashCode(x) may still return the same value for each of the
objects...

-- Anton.
 
M

Michael Borgwardt

Anton said:
It seems that the identityHashCode() is no better than the non-overridden
hashCode()... Still, if two objects 'x' are different the
identityHashCode(x) may still return the same value for each of the
objects...

Yes, that's true. But it's pretty much the only way there is to generate an
"object ID". Certainly one should not base production code on it being
always different for different objects, but in this case, where it's only
used for debugging, it should be sufficient.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top