Summary
The enhanced for loop, introduced in Java 5, adds a simplified syntax for iterations over collections. Nishanth Sastry summarizes key features of this construct in a recent java.net article.
Advertisement
Sun's official language guide for new JDK 5 features states that,
Iterating over a collection is uglier than it needs to be.
The for-each loop, or enhanced for loop, was introduced in JDK 5 to reduce some of the clutter, and possible errors, in iterating over collections. Nishanth Sastry summarizes key features of the enhanced for loop in a recent java.net article, Nuances of the Java 5.0 for-each Loop, concluding that:
In effect, the for-each loop is nothing more than syntactic sugar. It makes the code much easier to read, but provides no new core functionality. In fact, the Java Language Specification describes the meaning of the new for-each loop in terms of an equivalent basic for loop. Essentially, an intermediate Iterator of the right kind is created, and it is guaranteed to be an identifier different from all other user variables in the scope.
Sastry summarizes several features of the enhanced for-each loop:
The for-each loop allows only one iteration variable, which precludes simultaneously iterating over two or more arrays or collections.
Nested iteration is possible.
Beware of auto-unboxing... Unless you are forced to use native types because of their support for arithmetic operations, it is preferable to use subclasses of Number (Integer, Double, etc.) in loop variables rather than the equivalent native types (int, double, etc.).
Watch out for null pointers... This particularly insidious error happens because of the hidden compiler-generated call to get the iterator. To make the code robust ... preface the for-each loop with a check like:
if (null != liszt)
for(int num : liszt)
//for loop body...
Iterating over varargs. Java 5.0 now allows a variable number of arguments of a single type to be passed in as the last parameter to a method. The compiler collects these varargs parameters into an array of that type. Typically, the body of the method treats the varargs as an array and iterates over them using a for-each loop.
Do not modify the list during iteration.
Consider returning an Iterable rather than a List. Even if you are not defining a custom iterator, if a method in your API is returning a List that is expected to be solely used in for-each loops, consider changing the return type to java.lang.Iterable instead. This effectively hides the details of the current implementation and allows for later optimizations such as lazy construction of each iterated item.
When appropriate, implement java.util.RandomAccess to allow for compiler optimizations.
What do you think of the enhanced for loop? Do you routinely replace traditional for loops in old code with the enhanced for loop?
These features have only been documented for what, 3 years now?
I don't really understand the cottage industry around blogging about old news.
> What do you think of the enhanced for loop? Do you > routinely replace traditional for loops in old code with > the enhanced for loop?
If I'm using 1.5 (which I rarely can) I'll use it most of the time but I think some people get hung up on trying to use it in situations where they should just use the old style.
Interesting is the interface java.util.RandomAccess.
I had thought about interfaces without body before but was not sure they were used in practice. This is an example were the type system is not used for correctness but for optimization. It strengthens my suspicion that in practice type information in most programming languages is just metadata used for any purpose the programmer or compiler implementer sees fit.
nes wrote It strengthens my suspicion that in practice type information in most programming languages is just metadata used for any purpose the programmer or compiler implementer sees fit.
I don't know about that but we could generalize it into a truism - whatever is in a programming language will be used for any purpose the programmer sees fit.
> Interesting is the interface java.util.RandomAccess. > > I had thought about interfaces without body before but was > not sure they were used in practice. This is an example > were the type system is not used for correctness but for > optimization. It strengthens my suspicion that in practice > type information in most programming languages is just > metadata used for any purpose the programmer or compiler > implementer sees fit.
I would just caution you that some of the assertions made by the article author about compiler optimizations around RandomAccess are dubious.
RandomAccess was introduced so that library authors could use positional access with Lists like ArrayList and avoid O(n^2) operations if the List was a LinkedList or other sequential access structure.