Example code:
int a[] = new int[]{0, 1, 2, 3};
int result = 0;
for (int i : a)
result += i;
Is the loop guaranteed to iterate across
According to the JLS, The enhanced for statement, your for-loop is equivalent to
int[] array = a;
for (int index = 0; index < a.length; index++) {
int i = array[index];
result += i;
}
"where array
and index
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." (slightly paraphrasing the variable names here).
So yes: the order is absolutely guaranteed.