Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How foreach loop works  RSS feed

 
Luke Moor
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about foreach loop:

Are they equivalent to each other? If so, I noticed that during the loop a variable(in this case "s") is created. Does that make the foreach loop consume more memory or affect its performance?
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those two loops do the same thing, yes. And if you look closely you'll see that both versions use a variable called "s", so that isn't going to make any difference. Not that creating a variable which takes 4 bytes of the billion or so bytes your code has access to is likely to be important, anyway.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except String[] does not have iterator() method.
 
John Pacuta
Greenhorn
Posts: 26
1
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Time for some Greenhorn to Greenhorn...

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.



Conclusion:
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.

Sources

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/
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, if you're talking about an array, a more appropriate comparison would be this:


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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!