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

5
  • What is the reason you want to know it? The JVM and the optimizer can do it as it wants to do it. Commented May 12, 2015 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. Commented May 12, 2015 at 11:18
  • Did you measure the results? I've wrote a simple test with 100k iterations, both with the same result Commented May 12, 2015 at 11:25
  • 1
    Saw here the same question: stackoverflow.com/questions/1006395/… Commented May 12, 2015 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. Commented May 12, 2015 at 11:36

2 Answers 2

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");
    }

}
Sign up to request clarification or add additional context in comments.

Comments

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...
   ......
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.