IMO, the new for loop syntax really shines in nested loops. Compare:
for (Iterator iter = orders.iterator(); iter.hasNext(); ) {
Order o = iter.next();
for (Iterator iter2 = o.lineItems().iterator(); iter.hasNext(); ) {
totalCost += iter2.next().price();
}
}
for (Order o : orders) {
for (LineItem i : o.lineItems()) {
totalCost += i.price();
}
}
Readability is a benefit here, but the significant benefit in my opinion is that the new syntax avoids the easy-to-overlook bug in the first example. Nested-loop bugs are a real difficulty, and something that can only be guarded against with good testing, or potentially an IDE that warns about the problem. FTR, of the three IDEs (emacs, vim, IntelliJ) that I use regularly, none of them give me any help on that first example.
-Patrick
--
Patrick Linskey
http://solarmetric.com