1

Sorry if it was already asked, but I did not find an answer on stackoveflow and I didn't see official tutorials about this field.

The question is in the title - if we have code like

int[] array = new int[20];
for (int el : array) {
   ...
}

Will JVM run it as standard for loop

for (int i = 0; i < array.length; i++)

Or it will create an iterator?

UPD This link may be helpful to complete the answer

Fastest way to iterate an Array in Java: loop variable vs enhanced for statement

Community
  • 1
  • 1
AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76
  • What is the reason you want to know it? The JVM and the optimizer can do it as it wants to do it. – chokdee May 12 '15 at 11:12
  • @chokdee it is about optimization. I have a method that runs over predefined list of strings, and checks if this list has an item, that contains a particular string. This method is used as a predicate on a big application cache (10k-100k items), so we found that JVM creates thousands of iterators per traverse. Now it will be re-written to array and I wonder about iterator instantiation. – AdamSkywalker May 12 '15 at 11:18
  • Did you measure the results? I've wrote a simple test with 100k iterations, both with the same result – chokdee May 12 '15 at 11:25
  • 1
    Saw here the same question: http://stackoverflow.com/questions/1006395/fastest-way-to-iterate-an-array-in-java-loop-variable-vs-enhanced-for-statement – chokdee May 12 '15 at 11:27
  • @chokdee Thanks for the link, I knew I need bytecode to verify it, I just didn't want to waste time on things, that might already be checked by somebody. – AdamSkywalker May 12 '15 at 11:36

2 Answers2

2

Arrays in Java don't implement Iterable interface so you cannot get an iterator from an array. Also you cannot have an Iterator over primitive types such as int because a primitive type can't be a generic parameter. E.g. if you want an Iterator, you have to use an Iterator instead, which will result in a lot of autoboxing and -unboxing if that's backed by an int[].

The JVM is unlikely to create iterator for for-each loop over arrays as it would be very inefficient.

In fact the compiler will transform the for-each loop into indexed for loop. You can check it by viewing the decompiled byte code for this method:

Source code:

public void test() {
    int[] array = new int[20];
    for (int el : array) {
        System.out.println("test");
    }
}

Decompiled byte-code:

public void test() {
    int[] array = new int[20];
    int[] var2 = array;
    int var3 = array.length;

    for(int var4 = 0; var4 < var3; ++var4) {
        int var10000 = var2[var4];
        System.out.println("lol");
    }

}
medvedev1088
  • 3,645
  • 24
  • 42
0

For-each loop is an iterable form of ordinary for loop, which is a better built data structure.

cars is an array, then for eg: -

  for(Car car: cars){
       //car.name() = audi...
       ....
        }

is actually the same code shown below...

for(Iterator<Car> iterator = cars.iterator(); iterator.hasNext(); ){
   Car car = iterator.next();
   //car.name() = audi...
   ......
}
shijin
  • 2,998
  • 1
  • 24
  • 30