Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>About <a href="http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2" rel="noreferrer">the enhanced for statement</a>, the Java Language Specifications writes:</p> <blockquote> <p>The enhanced for statement has the form:</p> <pre><code>EnhancedForStatement: for ( VariableModifiersopt Type Identifier: Expression) Statement </code></pre> <p>The Expression must either have type <code>Iterable</code> or else it must be of an array type (§10.1), or a compile-time error occurs.</p> <p>The scope of a local variable declared in the FormalParameter part of an enhanced <code>for</code> statement (§14.14) is the contained Statement</p> <p>The meaning of the enhanced <code>for</code> statement is given by translation into a basic <code>for</code> statement.</p> <p>If the type of <em><code>Expression</code></em> is a subtype of <code>Iterable</code>, then let <em><code>I</code></em> be the type of the expression <em>Expression.</em><code>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 #i = Expression.iterator(); #i.hasNext(); ) { VariableModifiersopt Type Identifier = #i.next(); Statement } </code></pre> <p>Where <em><code>#i</code></em> is a compiler-generated identifier that is distinct from any other identifiers (compiler-generated or otherwise) that are in scope (§6.3) at the point where the enhanced for statement occurs.</p> <p>Otherwise, the Expression necessarily has an array type, <em><code>T[]</code></em>. Let <em><code>L1 ... Lm</code></em> be the (possibly empty) sequence of labels immediately preceding the enhanced <code>for</code> statement. Then the meaning of the enhanced for statement is given by the following basic <code>for</code> statement:</p> <pre><code>T[] a = Expression; L1: L2: ... Lm: for (int i = 0; i &lt; a.length; i++) { VariableModifiersopt Type Identifier = a[i]; Statement } </code></pre> <p>Where <em>a</em> and <em>i</em> 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 for statement occurs.</p> </blockquote> <p>So in your case, <code>genArray()</code> doesn't return a subtype of <code>Iterable</code> but an array type, so your enhanced <code>for</code> statement is equivalent to the following basic <code>for</code> statement:</p> <pre><code>String[] a = genArray(); ... for (int i = 0; i &lt; a.length; i++) { String s = a[i]; // ... } </code></pre> <p>And <code>genArray()</code> will thus be called only once (but the currently accepted answer is partially wrong).</p>
    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. 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.
    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