Your question got me thinking about the simplest way to represent the difference between a foreach loop and a while loop. I think the message becomes cloudy when you use the Iterator. Is there a way to do this without an Iterator? I noticed you used varargs when you declared your array of Strings. I'm going to step through your code and try to explain it, mostly as a learning exercise.
The methods are very different syntactically but they achieve the same result: printing every element of an array. The choice of which is "better" seems to be based on comfort. As a rule I avoid the while loop, but I'm a Greenhorn after all.
1. Varargs, http://www.programcreek.com/2014/01/java-varargs-examples/
2. Iterator, https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html
3. Return an empty iterator, http://www.leveluplunch.com/java/examples/return-empty-iterator-instead-of-null/
John Pacuta wrote:The methods are very different syntactically but they achieve the same result: printing every element of an array. The choice of which is "better" seems to be based on comfort.
Yes, comfort has something to do with it but it's more about something that you mentioned specifically in another post: readability. I use the same term you did: a story. What story does the for-each loop tell vs. the while-loop?
The while-loop is like your grandmother (no offense to Nana) who, when asked how she is this morning, will go into excruciating detail about what has happened since she woke up earlier. The for-each loop, on the other hand, is like your grandpa, who'll simply say "Good."
It's all about the level of detail that's revealed in each story. I realize right now, I'm being a bit long-winded like Nana. Anyway, the while-loop gives you all the nitty-gritty details of how the collection of things being iterated on is accessed. The for-each loop hides all those details in the background and just gets right down to the business of giving you the thing you're most interested in: each element. No fuss, no muss.
The for-loop is "syntactic sugar", which means it's something that makes your code sweeter, easier to digest. It's eye candy in that it's much easier on the eyes than the more verbose while-loop. I'll shut up now, lest you call me a while-loop.
John Pacuta wrote:As a rule I avoid the while loop...
Given the choice between a while-loop and an equivalent for-each, that's a good rule of thumb: Prefer a for-each loop
John Pacuta wrote:but I'm a Greenhorn after all.
Your insights belie that. You may be new around these parts but I don't think this is your first rodeo.
One final note: Strive for clarity of intent and readability above all. Don't worry about memory usage or performance. That is the least of your worries. Of course, until it isn't. But it usually takes a lot of things to come together before any of that makes any difference. When it does come down to that, don't optimize based on your gut feeling. Your digestive tract is unreliable when it comes to making judgements about performance. Use a profiler instead.
Heed the advice of an old timer. "Nana" out.
P.S. I'm really just an old, long-winded cowpoke