<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Getting A Java Object&#8217;s Reference ID</title>
	<atom:link href="http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/</link>
	<description>Rod Hilton&#039;s rants about stuff he cares about way too much.</description>
	<lastBuildDate>Sat, 28 Jan 2012 12:01:26 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
	<item>
		<title>By: Ramakrishna</title>
		<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/comment-page-1/#comment-91163</link>
		<dc:creator>Ramakrishna</dc:creator>
		<pubDate>Thu, 20 Oct 2011 10:28:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomachetejuggling.com/?p=129#comment-91163</guid>
		<description>Thanx a lottttttt Frank....its working fine....i was searching for unique ids for objects.toHexString is better convincing....</description>
		<content:encoded><![CDATA[<p>Thanx a lottttttt Frank&#8230;.its working fine&#8230;.i was searching for unique ids for objects.toHexString is better convincing&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: vladimir knajtner</title>
		<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/comment-page-1/#comment-72312</link>
		<dc:creator>vladimir knajtner</dc:creator>
		<pubDate>Thu, 28 Apr 2011 21:16:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomachetejuggling.com/?p=129#comment-72312</guid>
		<description>Clumsy and inefficient. Oracle/Sun guys, have you ever tried to use this tool and take heap dumps on a very large (64bit) VM memory?  It takes hours to do something like that.  Instead your MemoryLeakClass, you usually find a String or some other simple object that has highest object count.  Sorry, but this toolkit works only on a mickey mouse hello world examples, not in real life.  Instead, how about exposing a memory profiling tools via JRE/JDK that can be used via debugger? For example,  providing Class.allInstances() or Object.allOwners() would be a great and quick way to debug most of memory related issues.  How about getting an expression profiler that can be launched from debugger?  All this stuff has been a part of grand daddy Smalltalk for decades, why not implement it in Java?</description>
		<content:encoded><![CDATA[<p>Clumsy and inefficient. Oracle/Sun guys, have you ever tried to use this tool and take heap dumps on a very large (64bit) VM memory?  It takes hours to do something like that.  Instead your MemoryLeakClass, you usually find a String or some other simple object that has highest object count.  Sorry, but this toolkit works only on a mickey mouse hello world examples, not in real life.  Instead, how about exposing a memory profiling tools via JRE/JDK that can be used via debugger? For example,  providing Class.allInstances() or Object.allOwners() would be a great and quick way to debug most of memory related issues.  How about getting an expression profiler that can be launched from debugger?  All this stuff has been a part of grand daddy Smalltalk for decades, why not implement it in Java?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/comment-page-1/#comment-69879</link>
		<dc:creator>Frank</dc:creator>
		<pubDate>Sun, 06 Mar 2011 16:41:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomachetejuggling.com/?p=129#comment-69879</guid>
		<description>Further to my comment, here is a little piece of code that shows how likely it is indeed that dissimilar objects with the same hash code are produced.

import java.util.Hashtable;
import java.util.Map;

public class HashcodeTest{
   static class DummyObject extends Object { }

   public static void reportClash(DummyObject obj1, DummyObject obj2) {
      System.out.println(&quot;obj1.hashCode() = &quot; + obj1.hashCode());
      System.out.println(&quot;obj2.hashCode() = &quot; + obj2.hashCode());
      System.out.println(&quot;(obj1 == obj2) = &quot; + (obj1 == obj2) + &quot; (!)&quot;);   
   }

   public static void main(String[] args) {
      Map map = new Hashtable();
      for (int count = 1; true; count++) {
         DummyObject obj = new DummyObject();
         if (map.containsKey(obj.hashCode())) {
            System.out.println(
               &quot;Clash found after instantiating &quot; + count + &quot; objects.&quot;);
            reportClash(map.get(obj.hashCode()), obj);
            System.exit(0);
         }
         map.put(obj.hashCode(), obj);
      }
   }
}

It repetitively constructs objects, stores them in a Map using their hash code as a key, and detects when a key has already been used and thus two different objects with the same hashCode() result coexists. On my system (Java 6, Ubuntu 10.04) this happens only after 1756 instantiations. The mathematically-inclined might work out the average number of instantiations as a special instance of the birthday paradox, supposing hashCode() aims to be randomly distributed. This confirms, at least for me, that hashCode() is not even in *practical terms* a useful facility to serve as an object identity.

PS: If anyone wants to share some ideas please feel free to get in touch, my email is on my University of York website (http://www-users.cs.york.ac.uk/~zeyda/).

Best wishes,
Frank</description>
		<content:encoded><![CDATA[<p>Further to my comment, here is a little piece of code that shows how likely it is indeed that dissimilar objects with the same hash code are produced.</p>
<p>import java.util.Hashtable;<br />
import java.util.Map;</p>
<p>public class HashcodeTest{<br />
   static class DummyObject extends Object { }</p>
<p>   public static void reportClash(DummyObject obj1, DummyObject obj2) {<br />
      System.out.println(&#8220;obj1.hashCode() = &#8221; + obj1.hashCode());<br />
      System.out.println(&#8220;obj2.hashCode() = &#8221; + obj2.hashCode());<br />
      System.out.println(&#8220;(obj1 == obj2) = &#8221; + (obj1 == obj2) + &#8221; (!)&#8221;);<br />
   }</p>
<p>   public static void main(String[] args) {<br />
      Map map = new Hashtable();<br />
      for (int count = 1; true; count++) {<br />
         DummyObject obj = new DummyObject();<br />
         if (map.containsKey(obj.hashCode())) {<br />
            System.out.println(<br />
               &#8220;Clash found after instantiating &#8221; + count + &#8221; objects.&#8221;);<br />
            reportClash(map.get(obj.hashCode()), obj);<br />
            System.exit(0);<br />
         }<br />
         map.put(obj.hashCode(), obj);<br />
      }<br />
   }<br />
}</p>
<p>It repetitively constructs objects, stores them in a Map using their hash code as a key, and detects when a key has already been used and thus two different objects with the same hashCode() result coexists. On my system (Java 6, Ubuntu 10.04) this happens only after 1756 instantiations. The mathematically-inclined might work out the average number of instantiations as a special instance of the birthday paradox, supposing hashCode() aims to be randomly distributed. This confirms, at least for me, that hashCode() is not even in *practical terms* a useful facility to serve as an object identity.</p>
<p>PS: If anyone wants to share some ideas please feel free to get in touch, my email is on my University of York website (<a href="http://www-users.cs.york.ac.uk/~zeyda/" rel="nofollow">http://www-users.cs.york.ac.uk/~zeyda/</a>).</p>
<p>Best wishes,<br />
Frank</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/comment-page-1/#comment-69858</link>
		<dc:creator>Frank</dc:creator>
		<pubDate>Sun, 06 Mar 2011 04:06:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomachetejuggling.com/?p=129#comment-69858</guid>
		<description>PS: &quot;if so returns null&quot; should be &quot;if so returns 0&quot;.

PPS: predictable in a sense as independent of the usage pattern of the class. Maybe static is a better word...</description>
		<content:encoded><![CDATA[<p>PS: &#8220;if so returns null&#8221; should be &#8220;if so returns 0&#8243;.</p>
<p>PPS: predictable in a sense as independent of the usage pattern of the class. Maybe static is a better word&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/comment-page-1/#comment-69857</link>
		<dc:creator>Frank</dc:creator>
		<pubDate>Sun, 06 Mar 2011 04:03:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomachetejuggling.com/?p=129#comment-69857</guid>
		<description>Yes, sorry to say but your analysis seems a bit confused. First of all, toString() has no significance here as it does not have any notable contract. Also needless to say it is a bad idea to make assumptions about its value - best to leave it out of the game for its lack of a precise semantics.

Secondly, as the previous couple of posts already state, there is no requirement for hashCode() to yield a unique identifier for an object; a quick look at the method description in the Object class in the Java API confirms this.

  http://download.oracle.com/javase/6/docs/api/

But, of course, we *can* obtain a unique identifier for an object: its simply its object reference. The only problem is that we cannot do much with it :D. I.e. we can only check if references are equal or not, but not convert them into numbers as far as I am aware.
This seems only restrictive I believe if we attempt to implement something like a &quot;universal comparator&quot; that works for any kind of object, namely because we cannot do arithmetic comparison with references that would easily induce a total order that the comparator could use to fulfill its contract. Despite I think this problem can be solved.

A hint: one might implement a comparator that first checks if o1.equals(o2) and if so returns null. If not, it stores both objects in a map by assigning consecutive numbers to them when they are first encountered. This induces a total order that the comparator may use to fulfill its contract. The order is not a priori fixed or predictable, but in some case we don&#039;t actually care about that. Also, unlike something like: o1.hashCode() &lt; o2.hashCode() ? 1 : -1 it is consistent with equals. Any more thoughts on this are welcome.

PS: Just my two pence.

Frank</description>
		<content:encoded><![CDATA[<p>Yes, sorry to say but your analysis seems a bit confused. First of all, toString() has no significance here as it does not have any notable contract. Also needless to say it is a bad idea to make assumptions about its value &#8211; best to leave it out of the game for its lack of a precise semantics.</p>
<p>Secondly, as the previous couple of posts already state, there is no requirement for hashCode() to yield a unique identifier for an object; a quick look at the method description in the Object class in the Java API confirms this.</p>
<p>  <a href="http://download.oracle.com/javase/6/docs/api/" rel="nofollow">http://download.oracle.com/javase/6/docs/api/</a></p>
<p>But, of course, we *can* obtain a unique identifier for an object: its simply its object reference. The only problem is that we cannot do much with it :D. I.e. we can only check if references are equal or not, but not convert them into numbers as far as I am aware.<br />
This seems only restrictive I believe if we attempt to implement something like a &#8220;universal comparator&#8221; that works for any kind of object, namely because we cannot do arithmetic comparison with references that would easily induce a total order that the comparator could use to fulfill its contract. Despite I think this problem can be solved.</p>
<p>A hint: one might implement a comparator that first checks if o1.equals(o2) and if so returns null. If not, it stores both objects in a map by assigning consecutive numbers to them when they are first encountered. This induces a total order that the comparator may use to fulfill its contract. The order is not a priori fixed or predictable, but in some case we don&#8217;t actually care about that. Also, unlike something like: o1.hashCode() &lt; o2.hashCode() ? 1 : -1 it is consistent with equals. Any more thoughts on this are welcome.</p>
<p>PS: Just my two pence.</p>
<p>Frank</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dontell</title>
		<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/comment-page-1/#comment-61691</link>
		<dc:creator>Dontell</dc:creator>
		<pubDate>Wed, 07 Jul 2010 07:08:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomachetejuggling.com/?p=129#comment-61691</guid>
		<description>&quot;UPDATE: A number of commenters have pointed out that I’m totally wrong here. Please don’t do this.&quot;

This is so funny, really :-D.</description>
		<content:encoded><![CDATA[<p>&#8220;UPDATE: A number of commenters have pointed out that I’m totally wrong here. Please don’t do this.&#8221;</p>
<p>This is so funny, really :-D.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: infinity0</title>
		<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/comment-page-1/#comment-51374</link>
		<dc:creator>infinity0</dc:creator>
		<pubDate>Tue, 14 Jul 2009 13:13:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomachetejuggling.com/?p=129#comment-51374</guid>
		<description>The blog post is *wrong*. System.identityHashCode() does NOT generate unique IDs for objects. Its only guarantee is that if the codes are different the objects are different, not the other way round. If you rely on this behaviour, your program will see very weird bugs every once in a while.</description>
		<content:encoded><![CDATA[<p>The blog post is *wrong*. System.identityHashCode() does NOT generate unique IDs for objects. Its only guarantee is that if the codes are different the objects are different, not the other way round. If you rely on this behaviour, your program will see very weird bugs every once in a while.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Petr Smid</title>
		<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/comment-page-1/#comment-48178</link>
		<dc:creator>Petr Smid</dc:creator>
		<pubDate>Thu, 21 May 2009 15:48:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomachetejuggling.com/?p=129#comment-48178</guid>
		<description>hashCode gives you hash code and NOT any unique ID! Hash code can have colisions. Btw. see API documentation of Object.hashCode().
Whatmore I see no reason to override hashCode() when overriding toString(). ToString method is not ment to return hashCode... just String describing the current instance.</description>
		<content:encoded><![CDATA[<p>hashCode gives you hash code and NOT any unique ID! Hash code can have colisions. Btw. see API documentation of Object.hashCode().<br />
Whatmore I see no reason to override hashCode() when overriding toString(). ToString method is not ment to return hashCode&#8230; just String describing the current instance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dom Sparks</title>
		<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/comment-page-1/#comment-47258</link>
		<dc:creator>Dom Sparks</dc:creator>
		<pubDate>Wed, 06 May 2009 16:35:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomachetejuggling.com/?p=129#comment-47258</guid>
		<description>Agree with the previous comment, and in fact I&#039;m currently looking at some code which is problematic because System.identityHashCode() is returning the same hashcode for two distinct objects.  Although I&#039;m sure that previous versions of Sun&#039;s JDK would always produce unique values, it seems that in JDK1.5 (which is what I&#039;m currently using) the value is not necessarily unique.  I&#039;m having to change my code so that I generate my own ids for objects as and when they are created, rather than using System.identityHashCode() as an identifier...</description>
		<content:encoded><![CDATA[<p>Agree with the previous comment, and in fact I&#8217;m currently looking at some code which is problematic because System.identityHashCode() is returning the same hashcode for two distinct objects.  Although I&#8217;m sure that previous versions of Sun&#8217;s JDK would always produce unique values, it seems that in JDK1.5 (which is what I&#8217;m currently using) the value is not necessarily unique.  I&#8217;m having to change my code so that I generate my own ids for objects as and when they are created, rather than using System.identityHashCode() as an identifier&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: redmaniac</title>
		<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/comment-page-1/#comment-44051</link>
		<dc:creator>redmaniac</dc:creator>
		<pubDate>Sun, 22 Mar 2009 19:22:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomachetejuggling.com/?p=129#comment-44051</guid>
		<description>You should be careful with taking the Object.hashCode for a reference id: According to the Sun Java API the class  &#039;Object&#039; implements hashCode in such a way that it really does distinguish any two objects. However, it also states that this behavior is by no means required by the Java Standard, i.e. another java implementation than Sun&#039;s might behave differently in that issue while still being a &quot;correct&quot; java implementation.</description>
		<content:encoded><![CDATA[<p>You should be careful with taking the Object.hashCode for a reference id: According to the Sun Java API the class  &#8216;Object&#8217; implements hashCode in such a way that it really does distinguish any two objects. However, it also states that this behavior is by no means required by the Java Standard, i.e. another java implementation than Sun&#8217;s might behave differently in that issue while still being a &#8220;correct&#8221; java implementation.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

