How does the Java for each loop work?

what is the use of the for each loop over the for loop?

1 Like

the for each loop is introduced in the java7

for(Iterator i = someList.iterator(); i.hasNext(); ) {

    String item =;



Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use the for( : ) idiom, since the actual Iterator is merely inferred.


As was noted by Denis Bueno, this code works for any object that implements the Iterable interface.