Java's Enhanced For Loop Mystery
I like Java 5’s new enhanced for loop. It makes the code that iterates over collections and arrays less verbose. Anything that reduces Java’s verbosity is a good thing in my book.
Yesterday I wondered if the Java 5 enhanced for loop calls the collection expression once or every time through the loop. I searched for the answer online, and couldn’t find one. Every example I found assigned the collection or array to a variable first and then used that variable in the expression, like this:
What I was looking for was a statement like, “the list expression is only evaluated once”. After a few minutes, I realized that it would be faster to write a small program to determine the answer. (See Just Try It.
With a simple
javac Test.java && java Test I had the answer:
stringCollection() is only called once.