<?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's views on programming, technology, and life.</description>
	<lastBuildDate>Sat, 04 Sep 2010 19:29:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<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>
	<item>
		<title>By: anonymous</title>
		<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/comment-page-1/#comment-43929</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Sat, 21 Mar 2009 01:01:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomachetejuggling.com/?p=129#comment-43929</guid>
		<description>I have been looking to get/print such info too.  And for a bit, the explanation here seemed convincing.  However, when I wrote a small program and when I force the VM to do a &quot;full thread dump&quot; (by hitting Ctrl-Brk on keyboard, or by running jmap and jhat, and look at the output, the instance addresses are totally different.  Try it out with this simple program.  If you look at the &quot;full thread dump&quot;, look for the main-thread stack and look for what object is in locked state - corresponding to synchronized state here.

Hope somebody has a way of really printing real object references.

public class Foo
{
	private String sName;

	Foo()
   	{
		;
	}

	Foo(String s)
   	{
		sName = s;
	}
			
	public static void main(String[] args) {
		Foo f1 = new Foo(&quot;Hello&quot;);
		Foo f2 = new Foo(&quot;Hello&quot;);


		Object syncObj = null;
		for(int i=0; i &lt; 50000; i++) {
			System.out.println(&quot;f1: &quot; + &quot;0x&quot; + Integer.toHexString(System.identityHashCode(f1)));
			System.out.println(&quot;f2: &quot; + &quot;0x&quot; + Integer.toHexString(System.identityHashCode(f2)));
			System.out.println(&quot;Class f1: &quot; + &quot;0x&quot; + Integer.toHexString(System.identityHashCode(f1.getClass())));

			try {
				int j = i % 3;
				switch (j) {
					case 0: 
						syncObj = f1;
						System.out.println(&quot;Synchronizing on f1 obj&quot;);
						break;
					case 1:
						syncObj = f2;
						System.out.println(&quot;Synchronizing on f2 obj&quot;);
						break;
					case 2:
						syncObj = f1.getClass();
						System.out.println(&quot;Synchronizing on Class obj&quot;);
						break;
					default:
						syncObj = null;
						break;
					}
				synchronized(syncObj) {
					Thread.sleep(10000);
				}
			}
			catch (Exception e) { ; }
		}

	}
}</description>
		<content:encoded><![CDATA[<p>I have been looking to get/print such info too.  And for a bit, the explanation here seemed convincing.  However, when I wrote a small program and when I force the VM to do a &#8220;full thread dump&#8221; (by hitting Ctrl-Brk on keyboard, or by running jmap and jhat, and look at the output, the instance addresses are totally different.  Try it out with this simple program.  If you look at the &#8220;full thread dump&#8221;, look for the main-thread stack and look for what object is in locked state &#8211; corresponding to synchronized state here.</p>
<p>Hope somebody has a way of really printing real object references.</p>
<p>public class Foo<br />
{<br />
	private String sName;</p>
<p>	Foo()<br />
   	{<br />
		;<br />
	}</p>
<p>	Foo(String s)<br />
   	{<br />
		sName = s;<br />
	}</p>
<p>	public static void main(String[] args) {<br />
		Foo f1 = new Foo(&#8220;Hello&#8221;);<br />
		Foo f2 = new Foo(&#8220;Hello&#8221;);</p>
<p>		Object syncObj = null;<br />
		for(int i=0; i &lt; 50000; i++) {<br />
			System.out.println(&#8220;f1: &#8221; + &#8220;0x&#8221; + Integer.toHexString(System.identityHashCode(f1)));<br />
			System.out.println(&#8220;f2: &#8221; + &#8220;0x&#8221; + Integer.toHexString(System.identityHashCode(f2)));<br />
			System.out.println(&#8220;Class f1: &#8221; + &#8220;0x&#8221; + Integer.toHexString(System.identityHashCode(f1.getClass())));</p>
<p>			try {<br />
				int j = i % 3;<br />
				switch (j) {<br />
					case 0:<br />
						syncObj = f1;<br />
						System.out.println(&#8220;Synchronizing on f1 obj&#8221;);<br />
						break;<br />
					case 1:<br />
						syncObj = f2;<br />
						System.out.println(&#8220;Synchronizing on f2 obj&#8221;);<br />
						break;<br />
					case 2:<br />
						syncObj = f1.getClass();<br />
						System.out.println(&#8220;Synchronizing on Class obj&#8221;);<br />
						break;<br />
					default:<br />
						syncObj = null;<br />
						break;<br />
					}<br />
				synchronized(syncObj) {<br />
					Thread.sleep(10000);<br />
				}<br />
			}<br />
			catch (Exception e) { ; }<br />
		}</p>
<p>	}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lorenzo</title>
		<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/comment-page-1/#comment-42804</link>
		<dc:creator>Lorenzo</dc:creator>
		<pubDate>Mon, 02 Mar 2009 18:46:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomachetejuggling.com/?p=129#comment-42804</guid>
		<description>This saved my day! Thanks :-)</description>
		<content:encoded><![CDATA[<p>This saved my day! Thanks :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: david van brink</title>
		<link>http://www.nomachetejuggling.com/2008/06/04/getting-a-java-objects-reference-id/comment-page-1/#comment-39853</link>
		<dc:creator>david van brink</dc:creator>
		<pubDate>Fri, 02 Jan 2009 09:17:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomachetejuggling.com/?p=129#comment-39853</guid>
		<description>Thank you!

That clears up a mystery to me. Also solves my problem at hand (where I wanted to print a unique-ish id...)

Going further, in Eclipse, the debugger shows a different object id. Stuff like String[id=73] an other low numbers... any idea where *that* comes from? Guessing it&#039;s unique to its debugger (if it overrides all object-creation) or in the debug API.

ToHexString is AOK. Thanks again.</description>
		<content:encoded><![CDATA[<p>Thank you!</p>
<p>That clears up a mystery to me. Also solves my problem at hand (where I wanted to print a unique-ish id&#8230;)</p>
<p>Going further, in Eclipse, the debugger shows a different object id. Stuff like String[id=73] an other low numbers&#8230; any idea where *that* comes from? Guessing it&#8217;s unique to its debugger (if it overrides all object-creation) or in the debug API.</p>
<p>ToHexString is AOK. Thanks again.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
