53

The usual form the of for each loop is this:

for(Foo bar: bars){
    bar.doThings();
}

But if I want to retain bar until after the loop, I can not use the for each loop:

Foo bar = null;
// - Syntax error on token "bar", Identifier expected after this token
for(bar: bars){ 
    if(bar.condition())
        break;
}
bar.doThings();

The for loop gets the syntax error mentioned above.

Why is this? I'm not interested in workarounds, but just curious to the considerations behind this limitation.

In contrast, with an ordinary for loop, the variable can be declared outside or not at all...

int i = 1;
for(;i<max;i++){
    for(;;){
        // Do things
    }
}
2
  • 1
    you could actually make this line bold so it stands out more. "Why is this? I'm not interested in workarounds, but just curious to the considerations behind this limitation." Interesting question BTW +1.
    – Boro
    May 26, 2011 at 9:47
  • if bars is empty, you will get a NPE on bar.doThings(). The construct added nothing much to java with the exception: it allows swapping collections w/ Object[] (or concrete [] impl) w/o touching the code. This is my favorite part. There were always editors to write the iterator loop for you (ala for( : ))
    – bestsss
    May 29, 2011 at 20:16

4 Answers 4

30

This is a good question and I would be happy to see some in-depth answer. However, the official documentation says:

These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases.

The great majority of cases is the answer for me.

On a side note, personally, I think that foreach loop in Java is just a nice syntax for a standard iterator loop. So, the compiler creates the iterator for the structure and uses the variable to get the value for current iteration. To ensure that the variable has been initialised you need to declare it for the scope of loop (and I think this prevents the variable from being used somewhere else, e.g. in another thread). Because of that, you cannot use the variable after the loop. But, this is just my opinion and I would be very happy to hear from someone who knows it better. :)

Edit Here is an interesting article about foreach loops in Java.

Another edit I analysed (with jclasslib the bytecode of these methods:

 private static void testForEach(ArrayList<String> als) {
  for(String s: als)
    System.out.println(s);
 }

 private static void testIterator(ArrayList<String> als) {
  for(Iterator<String> is = als.iterator(); is.hasNext();) {
   String s = is.next();
   System.out.println(s);
  } 
 }

Both methods are represented by the same bytecode:

 0 aload_0
 1 invokevirtual #2 <java/util/ArrayList.iterator>
 4 astore_1
 5 aload_1
 6 invokeinterface #3 <java/util/Iterator.hasNext> count 1
11 ifeq 34 (+23)
14 aload_1
15 invokeinterface #4 <java/util/Iterator.next> count 1
20 checkcast #5 <java/lang/String>
23 astore_2
24 getstatic #6 <java/lang/System.out>
27 aload_2
28 invokevirtual #7 <java/io/PrintStream.println>
31 goto 5 (-26)
34 return

The difference is in line 1, the latter method uses invokevirtual #8. However, both invocations result in calling the same method of Iterator. So, it seems that foreach is nothing more than a syntactic sugar that the compiler just translates into a predefined construct, just as the documentation says. This does not answer the question why it is the way it is. I though this is just interesting and might be worth to know, in the context of the discussion.

2
  • I like this answer. Probably that is the reason. @Waldheinz @Puce @Sorrow @Manny +1 for you guys.
    – Boro
    May 26, 2011 at 10:04
  • 3
    Interestingly, the situation I mention here is not listed as one of these shortcommings. They only mention shortcommings where direct access to the iterator is required...
    – Stim
    May 26, 2011 at 11:16
4

I guess they just wanted to promote "good practices": from the Java language guide:

These shortcomings were known by the designers, who made a conscious decision to go with a clean, simple construct that would cover the great majority of cases.

As we know, the behaviour you are asking for can be simulated by just expanding the for-each loop to the old iterator - based style, and probably they just thought its Good Enough.

3
  • Beat me to it! I just read through that document and was going to quote the same sentence.
    – BudgieInWA
    May 26, 2011 at 10:04
  • 2
    Interestingly, the situation I mention here is not listed as one of these shortcommings.
    – Stim
    May 26, 2011 at 10:07
  • 1
    the list of shortcomings does not include an inability to declare the otherwise-local variable external to the loop Dec 1, 2015 at 17:25
3

The way to iterate over a list in this case is:

Foo bar = null;
for(Iterator<Foo> barIterator = bars.iterator(); barIterator.hasNext();){ 
   bar = barIterator.next(); // or whatever else you want to do
   ...
}

Don't iterate over a list using the index unless the list implements RandomAccess!

The enhanced for loop is just syntactic sugar for this iterator approach, which keeps the variable (here bar) local to the loop. (Keeping variables as local as possible is a good thing, generally.)

3
  • Yes, this would be a workaround, but I'm interested in why we are forced to use this workaround instead.
    – Stim
    May 26, 2011 at 10:02
  • Yes, sorry, I read the question too quickly. Actually, I like the idea you're mentioning, with declaring the variable name outside the loop. I don't see any issues and it would be aligned with the other loop variants. Maybe you want to file an enhancement request: bugs.sun.com
    – Puce
    May 26, 2011 at 10:06
  • This site is still using "sun.com"?
    – Puce
    May 26, 2011 at 10:07
1

The purpose of this new syntax is for beautification since it doesn't really add anything new to Java. My guess is they didn't allow the syntax you did for the sole reason: It's not beautiful! :)

2
  • Ah, but sometimes I need to iterate over several different collections, and the obvious variable name to choose is the same for each. With the current syntax, I have to give them different names. Sep 20, 2011 at 6:23
  • 1
    you can use the same name since the scope ends after the iteration.. unless you are iterating a collection inside another collection, which in any case, needs to have a different variable name
    – Manny
    Sep 21, 2011 at 3:06

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.