Re: super.toString() ?
Patricia Shanahan wrote:
Lew wrote:
Thomas Pornin wrote:
Just use this:
public static String objectToString(Object ref)
{
if (ref == null)
return "null";
int hc = System.identityHashCode(ref);
return ref.getClass().getName() + '@' + Integer.toHexString(hc);
}
That works, but it isn't guaranteed to.
Depends what you mean by "works". It is guaranteed, through Object's
toString documentation, to produce the same string as would be produced
by ref.toString() if ref had inherited the Object implementation of
toString(). There is no guarantee that two references with equal results
refer to the same object.
I was referring to the OP's original request,
"I want to see if they both are the same address."
This is not what the 'toString()' method provides. The code above satisfies
the request to get the equivalent of 'Object#toString()", but not to "see if
they both are the same address". As you say, "[t]here is no guarantee that
two references with equal results refer to the same object."
--
Lew