<?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: Iterator idioms</title>
	<atom:link href="http://tech.puredanger.com/index.php/2007/09/06/iterator-idioms/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech.puredanger.com/2007/09/06/iterator-idioms/</link>
	<description>Alex Miller&#039;s technical blog</description>
	<lastBuildDate>Mon, 06 Feb 2012 19:39:50 -0800</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
	<item>
		<title>By: Guillaume Poirier</title>
		<link>http://tech.puredanger.com/2007/09/06/iterator-idioms/comment-page-1/#comment-8085</link>
		<dc:creator>Guillaume Poirier</dc:creator>
		<pubDate>Wed, 12 Sep 2007 20:16:53 +0000</pubDate>
		<guid isPermaLink="false">http://tech.puredanger.com/2007/09/06/iterator-idioms/#comment-8085</guid>
		<description>There is actually a difference in the bytecode generated by the while and for idoms, it&#039;s just that it&#039;s subtle and doesn&#039;t show in your exemple.

Take this example:

- for idiom :

    List list = Arrays.asList(&quot;hello&quot;, &quot;world&quot;);
    for (String s : list) {
      System.out.println(s);
    }
    String name = &quot;World&quot;;
    String str = &quot;Hello, &quot; + name;
    System.out.println(str);

- while idom:

    List list = Arrays.asList(&quot;hello&quot;, &quot;world&quot;);
    Iterator it = list.iterator();
    while (it.hasNext()) {
      String s = it.next();
      System.out.println(s);
    }
    String name = &quot;World&quot;;
    String str = &quot;Hello, &quot; + name;
    System.out.println(str);


In that exemple, the generated bycode will differ, the while idom will require more memory on its stack frame for local variables than the for idom. The for idom uses 3 memory addresses and the while uses 4, because it cannot reuse the slot that was used for &quot;it&quot;. 

It&#039;s really a small difference though, the most important advantage of the for idom is really at compile time, that you you can safely reuse the &quot;it&quot; name for future variables and have it isolated from the one used by the control loop.</description>
		<content:encoded><![CDATA[<p>There is actually a difference in the bytecode generated by the while and for idoms, it&#8217;s just that it&#8217;s subtle and doesn&#8217;t show in your exemple.</p>
<p>Take this example:</p>
<p>- for idiom :</p>
<p>    List list = Arrays.asList(&#8220;hello&#8221;, &#8220;world&#8221;);<br />
    for (String s : list) {<br />
      System.out.println(s);<br />
    }<br />
    String name = &#8220;World&#8221;;<br />
    String str = &#8220;Hello, &#8221; + name;<br />
    System.out.println(str);</p>
<p>- while idom:</p>
<p>    List list = Arrays.asList(&#8220;hello&#8221;, &#8220;world&#8221;);<br />
    Iterator it = list.iterator();<br />
    while (it.hasNext()) {<br />
      String s = it.next();<br />
      System.out.println(s);<br />
    }<br />
    String name = &#8220;World&#8221;;<br />
    String str = &#8220;Hello, &#8221; + name;<br />
    System.out.println(str);</p>
<p>In that exemple, the generated bycode will differ, the while idom will require more memory on its stack frame for local variables than the for idom. The for idom uses 3 memory addresses and the while uses 4, because it cannot reuse the slot that was used for &#8220;it&#8221;. </p>
<p>It&#8217;s really a small difference though, the most important advantage of the for idom is really at compile time, that you you can safely reuse the &#8220;it&#8221; name for future variables and have it isolated from the one used by the control loop.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stefan Schulz</title>
		<link>http://tech.puredanger.com/2007/09/06/iterator-idioms/comment-page-1/#comment-7712</link>
		<dc:creator>Stefan Schulz</dc:creator>
		<pubDate>Fri, 07 Sep 2007 17:45:38 +0000</pubDate>
		<guid isPermaLink="false">http://tech.puredanger.com/2007/09/06/iterator-idioms/#comment-7712</guid>
		<description>Although not surprising, this is a very interesting insight to what happens with your Java code. It actually gives a hint that Java source code optimization (e.g. chaining calls) to reduce the number of lines does nothing but obfuscate your intentions. Maybe interesting to see some other code patterns. Like me, often assigning a pile of final variables before calling a method.</description>
		<content:encoded><![CDATA[<p>Although not surprising, this is a very interesting insight to what happens with your Java code. It actually gives a hint that Java source code optimization (e.g. chaining calls) to reduce the number of lines does nothing but obfuscate your intentions. Maybe interesting to see some other code patterns. Like me, often assigning a pile of final variables before calling a method.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://tech.puredanger.com/2007/09/06/iterator-idioms/comment-page-1/#comment-7705</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Fri, 07 Sep 2007 14:53:59 +0000</pubDate>
		<guid isPermaLink="false">http://tech.puredanger.com/2007/09/06/iterator-idioms/#comment-7705</guid>
		<description>Great question!  I should have mentioned it.  The tool you already have is &lt;a href=&quot;http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javap.html&quot; rel=&quot;nofollow&quot;&gt;javap&lt;/a&gt;, which is part of the JDK.  Javap take a class and will show the bytecode, signatures, etc.  You would run it here with something like:

javap -c -classpath ~/project test.TestIterator

The -c does disassembly (shows bytecode).  Otherwise, it will print method signatures by default.  There are other flags as help if you run javap -help.

The tool I actually used is the fantastic &lt;a href=&quot;http://asm.objectweb.org/eclipse/index.html&quot; rel=&quot;nofollow&quot;&gt;ASM ByteCode Outline plugin&lt;/a&gt; for Eclipse.  It lets you look at the bytecode for a selected method and can even show you the code to generate that bytecode using ASM, which is fairly indispensable when working at Terracotta. :)</description>
		<content:encoded><![CDATA[<p>Great question!  I should have mentioned it.  The tool you already have is <a href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javap.html" rel="nofollow">javap</a>, which is part of the JDK.  Javap take a class and will show the bytecode, signatures, etc.  You would run it here with something like:</p>
<p>javap -c -classpath ~/project test.TestIterator</p>
<p>The -c does disassembly (shows bytecode).  Otherwise, it will print method signatures by default.  There are other flags as help if you run javap -help.</p>
<p>The tool I actually used is the fantastic <a href="http://asm.objectweb.org/eclipse/index.html" rel="nofollow">ASM ByteCode Outline plugin</a> for Eclipse.  It lets you look at the bytecode for a selected method and can even show you the code to generate that bytecode using ASM, which is fairly indispensable when working at Terracotta. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: B. K. Oxley (binkley)</title>
		<link>http://tech.puredanger.com/2007/09/06/iterator-idioms/comment-page-1/#comment-7702</link>
		<dc:creator>B. K. Oxley (binkley)</dc:creator>
		<pubDate>Fri, 07 Sep 2007 14:01:25 +0000</pubDate>
		<guid isPermaLink="false">http://tech.puredanger.com/2007/09/06/iterator-idioms/#comment-7702</guid>
		<description>How does one generate the bytecode?  Which JDK tool did you use?  (A blindspot in my Java education, obviously!)</description>
		<content:encoded><![CDATA[<p>How does one generate the bytecode?  Which JDK tool did you use?  (A blindspot in my Java education, obviously!)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

