Re: Can I compare references (in a sense of compareTo method)?
chucky wrote:
Is it possible somehow to get the address of an object and then
compare it?
As I stated in the title of this topic and in first paragraph of my
original post, I was intrested in comparing the addresses in sense of
ordering. So the page you linked does not answer the question neither
positively nor negatively.
Take note of what Patricia Shanahan wrote:
It isn't even guaranteed that an object has an address.
Even when an object does have an "address", that "address" can change during
the lifetime of the object, for example after a garbage collection.
So the default hashCode() does not necessarily involve adresses.
and cannot, really, for the reasons cited.
I think Sun made a huge mistake referring to "address" in the docs for
hashCode(). There is no meaningful numeric interpretation of an object's
"address" in the JVM. Even so, the Javadocs do use the terminology
"/converting/ the internal address of the object" (emph. added), indicating
that even with this undefined concept of "address" it isn't even necessarily a
direct correspondence.
The hashCode() normally will not change unless the contents of the object
change. That means any tenuous association between the object's "address",
whatever that is, and its hashCode() will be broken at the next GC, assuming
no intervening changes in the object's values.
And assuming the object hasn't been optimized away entirely, thus eliminating
its "address" altogether.
The point is that Java as a language deliberately prevents the programmer from
doing direct address manipulation, preferring object-oriented reference
semantics. You cannot in general produce a consistent "address" for an object.
--
Lew