For statements are essentially compiled into equivalent while loops.
for (int i = 0; i < 5; ++i) {
// ...
}
int i = 0;
while (i < 5) {
// ...
++i;
}
The for
and while
statements above both compile into the exact same bytecode.
0: iconst_0
1: istore_1
2: iload_1
3: iconst_5
4: if_icmpge 13
// ...
7: iinc 1, 1
10: goto 2
13: return
Enhanced for
loops over arrays compile into while loops that are almost identical to that of regular for
loops iterating over an entire array.
for (String value : arrayExpr) {
// ...
}
$array = arrayExpr;
$length = $array.length;
$i = 0;
while ($i < $length) {
String value = $array[$i];
// ...
++$i;
}
Note, that the result of the inital array expression needs to be stored in an unnamed variable, because the re-evaluation of the expression might not be side-effect free.
Enhanced for
loops over iterables compile into a while loop equivalent using the iterable / iterator api.
for (String value : iterable) {
// ...
}
$iterator = iterable.iterator(); // 1
while ($iterator.hasNext()) { // 2
String value = $iterator.next(); // 3
// ...
}
-
The
Iterator<T>
object is retrived. -
The loop will continue as long as
hasNext()
returnstrue
. -
The Iterator’s
next()
method is called to retrieve the current item and to advance the iterator.