12

Let's start with code.

Future<MyResult> obj = getFuture();
debugLog.println("Handling future: " + System.identityHashCode(obj);

and then somewhere else same again, or possibly the same piece of code executed again, possibly in different thread.

Future<MyResult> obj = getFuture();
debugLog.println("Handling future: " + System.identityHashCode(obj);

Now above, if obj's are same object, the debug prints will be same, obviously. But if they are different, there's still a chance of hash collision, so the printout may be same even for different objects. So having same output (or more generally, same string) does not guarantee same object.

Question: Is there a way in Java to get unique id string for arbitrary object? Or more formally, give static String Id.str(Object o) method so that so that this is always true:

final Object obj1 = ...;
final String firstId = Id.str(obj1);
// arbitrary time passes, garbage collections happen etc.
// firstId and obj1 are same variables as above
(firstId.equals(Id.str.obj2)) == (obj1 == obj2)
hyde
  • 60,639
  • 21
  • 115
  • 176

2 Answers2

17

I think actually there isn't any method (i.e. technique) which will guarantee you such a uniqueness of the object's ID. The only purpose of such an identification is to have a method for addressing this object's data in memory. In case when this object dies and then is removed from the memory by the GC, nobody will restrict the system to use its former address space for placing new object's data.

But in fact during some short period of time, among all available objects which have any references inside your program, System.identityHashCode(obj) will actually give you a unique identity of the object. This is because this hashCode is calculated using the object's in-memory location. However, it is an implementation feature, which is not documented and is not guaranteed for other JVMs.

Also, you can read this famous QA: Java: How to get the unique ID of an object which overrides hashCode()?

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
0

Assuming it is not important for this ID to actually be the hashcode then you could guarantee a unique ID by having a Singleton class generating ID's that are assigned to each object. You can then use that identifier rather than the hashcode.

What is your objective, perhaps there are better solitions?

codeghost
  • 1,014
  • 7
  • 14
  • And how you suggest to assign such ID's from singleton to objects, which will be created by 3d-party objects? – Andremoniy Jan 24 '13 at 11:38
  • Sorry, I must have missed where he said that, I was assuming he is defining the classes and can therefore put what he likes in them. – codeghost Jan 24 '13 at 11:40
  • Well, one purpose is what is implied in the question: looking at logs and seeing printed IDs. Obviously there are workarounds, such as printing some case-specific identifying data when such is available. But when type is something like `Future<>`, this is hard. So I'm asking if a generic solution exists. – hyde Jan 24 '13 at 12:24
  • Sorry, not meaning to extend this line, as clearly this isn't what you want, but given that Future is generic, I was inferring that it was the type of the returned result you cared about and *incorrectly* assumed those were going to be your objects. If you need this to work across any and all classes, especially including those you haven't produced then this is not possible and the likelihood of collision increases as you have no view of the hashcode implementations. – codeghost Jan 24 '13 at 12:28