25

I understand that new for each loop works with Iterable and arrays, but I don't know what goes behind the scenes when working with arrays.

Can anyone help me understand this? Thanks in advance.

int[] number = new int[10];

for(int i: number) {

}
Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
AS_
  • 369
  • 1
  • 5
  • 9
  • BTW, it would have helped if you had commented why the question was down voted. – AS_ Jan 10 '13 at 20:11
  • upvoted, if find it interesting, especially if its equivalent on byte code basis to a normal (efficient) loop. But this nobody has answered exactly. – AlexWien Jan 10 '13 at 21:14

7 Answers7

16

The loop is equivalent to:

for(int j = 0; j < number.length; j++) {
  int i = number[j];
  ...
}

where j is an internally generated reference that does not conflict with normal user identifiers.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
  • Is this true for arrays of reference types as well? E.g. will Java internally use this implementation on an Object[], or will it do something else (like convert it to an Iterable)? – Karakuri Jan 14 '15 at 16:37
  • @Karakuri I'm not even claiming this is how it is actually implemented for primitive arrays, just that the required behavior is equivalent. The required behavior for `Object[]` is also equivalent. – Patricia Shanahan Jan 14 '15 at 16:40
11

A bit late, but here it is.

The compiler knows if you are using the for-each loop statement for a collection or for an array.

If used for collection, the compiler translates the for-each loop to the equivalent for loop using an Iterator.

If used for an array, the compiler translates the for-each loop to the equivalent for loop using an index variable.

Here is a description at oracle.com

DwB
  • 37,124
  • 11
  • 56
  • 82
3

In your code, you allocate an array of 10 integers in the memory and obtain a reference to it. In the for-loop you simply iterate over every item in the array, which initially will be 0 for all the items. The value of every item will be stored in the variable i declared in your for-loop as you iterate the array elements.

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
2

this is equivalent to:

for(int x = 0; x < number.length; x++) {
  int i = number[x];
}
GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
1

This is the equivalent to:

final int len = number.length;
for(int j = 0; j < len; j++) {
  int i = number[j];
}

Note that the forEach will not evaluate the .length in each loop. This might be also be eliminated by the JVM, but especially in case of collections, where some would use

for(int j = 0; j < collection.size(); j++) {

it makes a (small) difference to the faster

int len = collection.size()
for(int j = 0; j < len; j++) {
AlexWien
  • 28,470
  • 6
  • 53
  • 83
0

The for each over arrays is essentially "sugar" over this construct:

for(int i = 0;i<number.length;i++)
{  
}

I would imagine this was provided as a construct of the language so that people could use the enhanced for loop over a structure that was iterated over in the old way.

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
  • That would give i the values 0 through length-1. It should be taking the values of the array elements, all 0 in the example code. – Patricia Shanahan Jan 10 '13 at 19:44
  • 2
    @PatriciaShanahan incorrect, there is no code inside my braces. I was explaining the construct that was occurring behind the sugar – Woot4Moo Jan 10 '13 at 19:46
0

IntStream.range(1,4) can be used, if using java 8.

Neuron
  • 5,141
  • 5
  • 38
  • 59
KayV
  • 12,987
  • 11
  • 98
  • 148