T
tony
use the time when the obj is created as its ID can ensure its uniqueness
I don't think the Sun JVM's have used object handles for many years. TheTwisted said:Regarding identityHashCode() -- I have it on good authority than the
Sun JVM implementation, and the typical implementation, uses the RAM
address of the object's handle (which isn't moved by compacting gc).
I don't think the Sun JVM's have used object handles for many years. The
early garbage collectors did work that way.
tony said:use the time when the obj is created as its ID can ensure its uniqueness
Plus, UUIDs are eminently printable, which was a requirement of the OP.
Twisted said:JVMs tend to either use handles, or they directly rewrite referring
objects' reference pointers. In the latter case, the object's own RAM
address is likely to be used to set its hash code when it's created,
but then the object may get moved and the hash code stay the same.
Nor is the "address" required to remain "constant" during the lifetime of the object, unlike the return value of hashCode().
Mark Thornton said:But not at all memorable. Using a printed UUID for any purpose is tedious.
Mark said:I don't think the Sun JVM's have used object handles for many years. The
early garbage collectors did work that way.
Tom said:Absolutely.
Handles were briefly resurrected for the earliest HotSpot
implementations. On typical, modern, serious Java implementations,
objects do get moved in memory and have no fixed handles.
Even for 32-bit JVMs, identity hash codes do clash. If you look at the
values produced, they clearly aren't sensible addresses - odd numbers,
for instance. I believe that it is true that values are typically
*derived* from the address of the object at the time the hash is first
requested.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6321873
I can't believe this nonsense keeps turning up...
It appears straightforward to find two live objects sharing the same identityHashCode, using only a bog standard JRE. Real implementations with unique identityHashCodes for live objects, I would expect to be confined to legacy J2ME JVMs (if they had System.identityHashCode).
This is typically implemented by converting the internal address of the object into an integer,
but this implementation technique is not required by the JavaTM programming language.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.
Hal Vaughan said:That's how I thought of it. I might have seen it before and forgotten it,
since it came so easily to me. I did this:
protected static int componentID = 0;
protected int myID = componentID++;
Andreas said:Are you aware, that this is *not* thread-safe?
If you were running multiple threads (which I don't know,
so this warning may be moot), then you risk getting non-unique
IDs!
What kind of safeguards does Java have in place so this doesn't overload my
CPU or RAM?
Andreas said:Are you aware, that this is *not* thread-safe?
If you were running multiple threads (which I don't know,
so this warning may be moot), then you risk getting non-unique
IDs!
The hash is almost unique.Is there any way to get a unique string or number for each object that is
created by a particular JVM?
Is there any way to get a unique string or number for each object that is
created by a particular JVM?
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.