• Advertisement
Sign in to follow this  

[java] while (...) Object o = it.next() or Object o while (...) o = it.next()

This topic is 4294 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey! I was just wondering if there is any difference in speed if one does, for example:
Iterator it = list.iterator();
while (it.hasNext()) {
    Object o = it.next();
}
compared to
Iterator it = list.iterator();
Object o;
while (it.hasNext()) {
    o = it.next();
}

Share this post


Link to post
Share on other sites
Advertisement
Probably not. I bet those guys at Sun optimized their compiler well enough to fix your bad code. Look for the big bottlenecks in your application, not these micro-optimizations.

Share this post


Link to post
Share on other sites
Don't micro-optimise, make your code readable.

for (Object o : list)
{
}

Share this post


Link to post
Share on other sites
Thanks!

Yes, that's what I would bet too, but I wanted to make sure.

And yes, the for-each loop in definatly nicer but I just wanted an example. Thanks again!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement