Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>On the semantics of enhanced <code>for</code> loop</h3> <p>Here is the relevant excerpts from the <em>Java Language Specification 3rd Edition</em>, slightly edited for clarity:</p> <blockquote> <h3><a href="http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2" rel="noreferrer">JLS 14.14.2 The enhanced <code>for</code> statement</a></h3> <p>The enhanced <code>for</code> statement has the form:</p> <pre><code>for ( Type Identifier : Expression ) Statement </code></pre> <p>If the type of <code>Expression</code> is an array type, <code>T[]</code>, then the meaning of the enhanced <code>for</code> statement is given by the following basic <code>for</code> statement:</p> <pre><code>T[] a = Expression; for (int i = 0; i &lt; a.length; i++) { Type Identifier = a[i]; Statement } </code></pre> <p>where <code>a</code> and <code>i</code> are compiler-generated identifiers that are distinct from any other identifiers (compiler-generated or otherwise) that are in scope at the point where the enhanced <code>for</code> statement occurs.</p> </blockquote> <p><em>So in fact the language does guarantee that</em> <code>Expression</code> <em>will only be evaluated once.</em></p> <p>For completeness, here's the equivalence when the <code>Expression</code> is of type <code>Iterable</code>:</p> <blockquote> <h3><a href="http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2" rel="noreferrer">JLS 14.14.2 The enhanced <code>for</code> statement</a></h3> <p>The enhanced <code>for</code> statement has the form:</p> <pre><code>for ( Type Identifier : Expression ) Statement </code></pre> <p>If the type of <code>Expression</code> is a subtype of <code>Iterable</code>, then let <code>I</code> be the type of the expression <code>Expression.iterator()</code>. The enhanced <code>for</code> statement is equivalent to a basic <code>for</code> statement of the form:</p> <pre><code>for (I iter = Expression.iterator(); iter.hasNext(); ) { Type Identifier = iter.next(); Statement } </code></pre> <p>where <code>iter</code> is a compiler-generated identifier that is distinct from any other identifiers (compiler-generated or otherwise) that are in scope at the point where the enhanced <code>for</code> statement occurs.</p> </blockquote> <p>Note that it is a compile-time error if <code>Expression</code> is neither an <code>Iterable</code> nor an array, so the above two are the only cases where you can use an enhanced <code>for</code> loop. Also, for clarity, the above quotes leave out information regarding any labels attached on the <code>for</code> loop and any modifiers attached on the <code>Identifier</code>, but these are handled as one would expect.</p> <hr> <h3>On the performance of enhanced <code>for</code> loop</h3> <p>Here's a quote from <em>Effective Java 2nd Edition, Item 46: Prefer for-each loops to traditional for loops</em></p> <blockquote> <p>The for-each loop, introduced in release 1.5, gets rid of the clutter and the opportunity for error by hiding the iterator or index variable completely. The resulting idiom applies equally to collections and arrays. <em>Note that there is no performance penalty for using the for-each loop</em>, even for arrays. In fact, it may offer a slight performance advantage over an ordinary <code>for</code> loop in some circumstances, as it computes the limit of the array index only once. While you <em>can</em> do this by hand, programmers don't always do so.</p> </blockquote> <p>Thus the book claims that in fact some compilers go beyond the JLS translation and performs additional optimization on the for-each loop (while still maintaining its semantics, of course).</p> <p>In summary, you should not worry about the performance of for-each loop. The specification by the language is sensible (<code>Expression</code> only evaluated once), and <em>precisely because this is the preferred construct in many scenarios</em>, compilers will make sure to optimize them as best they can.</p> <h3>See also</h3> <ul> <li><a href="http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/guide/language/foreach.html" rel="noreferrer">Java Language Guide/The for-each loop</a> <ul> <li>Has examples of idiomatic usage and how it can help minimize chances of errors</li> </ul></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload