The enhanced for
statement has the form
Enhanced For Statement | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
It is used to iterate over finish iterators,
iterable objects,
arrays, or the sequentially yielded values
(Section 16.1, “Generator Expressions and Sequential Evaluation”) of the expression.
The distinction is based on the type
I
of the expression, its
potential iteration type, and the specified type
and identifier, if any:
If I
is assignable to
de.grogra.xl.lang.FinishIterator
,
the potential iteration is over a finish iterator, and
the potential iteration type is the return type of the
public member method named value
of I
with no parameter,
or void
if no such method exists.
Otherwise, if I
has a public member method named
iterator
with no parameter
and return type java.util.Iterator
, the
potential iteration is over an iterable object, and the
potential iteration type is Object
,
or the explicitly specified type if this is
a reference type.
Otherwise, if I
is an array type,
the potential iteration is over an array, and
the potential iteration type is I
's component type.
Otherwise, the potential iteration is over the
sequentially yielded values of the expression, and the
potential iteration type is I
.
If no type has been specified explicitly in the
enhanced for
statement, or if the potential
iteration type is assignable to the explicitly specified type,
then the actual iteration and type are the potential iteration and type,
otherwise the actual iteration is over the sequentially
yielded values of the expression, and the actual iteration type
is I
.
If an identifier v
is specified in the enhanced for
statement, a local variable of that name is declared. Its scope
is the contained statement. It is a compile-time error if there
exists a local variable of the given name in the current scope.
The type T
and modifiers
m
of the local variable are the given type and modifiers, or the actual
type and final
if no type is specified.
It is a compile-time error if the
actual type is not assignable to the type of the variable.
Let e
be the expression of the
enhanced for
statement
and s
the contained statement,
then the execution of the statement is as follows:
An iteration over a finish iterator is equivalent to the following statement.
I #i = e; Throwable #t = null; try { while (#i.next()) { m T v = #i.value(); s } } catch (Throwable #u) { #t = #u; throw #u; } finally { #i.finish(#t); }
If no local variable is declared by the enhanced
for
statement, the line
m T v = #i.value();
has to be removed.
An iteration over an iterable object is equivalent to the following statement.
for (java.util.Iterator #i = e.iterator(); #i.hasNext(); ) { m T v = (T) #i.next(); s }
If no local variable is declared by the enhanced
for
statement, the line
m T v = #i.next();
has to be replaced by
#i.next();
.
An iteration over an array is equivalent to the iteration over the sequentially yielded values of its array generator expression (Section 16.6, “Array Generator Expressions”).
An iteration over the sequentially yielded values
of e
is executed by sequentially
evaluating the expression v = e
and executing
s
for every yielded result of
v = e
.
If no local variable is declared by the enhanced
for
statement, only the expression
e
is evaluated sequentially.
In the above definitions, the #
-prefixed identifiers
are compiler-generated identifiers that are distinct from any other
identifier.