Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Java foreach iteration order over primitives precisely defined?

Tags:

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 a[0], a[1], a[2], a[3] in that order? I strongly believe the answer is yes, but this page seems to not unambiguously state order.

Got a solid reference?

like image 297
dfrankow Avatar asked Mar 18 '09 21:03

dfrankow


People also ask

Does forEach in Java maintain order?

Core Java Tutorial The forEach() method is used to perform an action on each elements of the stream. If the forEach() method is used with parallel stream, the encounter order is not maintained. The action will be performed on each element, but their order will not be fixed.

Does iterator maintain order?

Iteration Order The order in which the elements contained in a Java Iterator are traversed depends on the object that supplies the Iterator . For instance, an iterator obtained from a List will iterate through the elements of that List in the same order the elements are stored internally in the List .

Which is faster traditional for loop or forEach in Java?

The FOR loop without length caching and FOREACH work slightly faster on arrays than FOR with length caching. Array. Foreach performance is approximately 6 times slower than FOR / FOREACH performance. The FOR loop without length caching works 3 times slower on lists, comparing to arrays.

Which is better forEach or for loop in Java?

The key difference between for Loop and foreach loop is that the for loop is a general purpose control structure while the foreach loop is an enhanced for loop that is applicable only to arrays and collections.


2 Answers

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.

like image 50
Joachim Sauer Avatar answered Sep 23 '22 03:09

Joachim Sauer


See section 14.14.2 of the Java Language Specification, 3rd edition.

If the type of Expression is a subtype of Iterable, then let I be the type of the expression Expression.iterator(). The enhanced for statement is equivalent to a basic for statement of the form:

for (I #i = Expression.iterator(); #i.hasNext(); ) {         VariableModifiersopt Type Identifier = #i.next();    Statement } 

Where #i 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.

like image 37
vladr Avatar answered Sep 25 '22 03:09

vladr