title: Printing unique object identity for debugging purposes in Java
body: <p>Let's start with code.</p>
  
  <pre><code>Future&lt;MyResult&gt; obj = getFuture();
  debugLog.println("Handling future: " + System.identityHashCode(obj);
  </code></pre>
  
  <p>and then somewhere else same again, or possibly the same piece of code executed again, possibly in different thread.</p>
  
  <pre><code>Future&lt;MyResult&gt; obj = getFuture();
  debugLog.println("Handling future: " + System.identityHashCode(obj);
  </code></pre>
  
  <p>Now above, if obj's are same object, the debug prints will be same, obviously. But if they are <em>different</em>, there's still a chance of hash collision, so the printout <em>may</em> be same even for different objects. So having same output (or more generally, same string) does not guarantee same object.</p>
  
  <p><strong>Question</strong>: Is there a way in Java to get unique id <em>string</em> for arbitrary object? Or more formally, give <code>static String Id.str(Object o)</code> method so that so that this is <em>always</em> true:</p>
  
  <pre><code>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)
  </code></pre>