This question has already been answered:

For some reason I didn’t find normal information on the Internet and google it on topics on stacker flou, but didn’t find the difference between for and for each in Java? It is clear that for syntactic sugar in Java and everything that a regular for can implement can be implemented on for each (e), and what else?

Reported as a duplicate by Anton Sorokin , Roman C , aleksandr barakin , Yuriy SPb java 26 Mar at 20:16 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • For uses an external iterator (a variable, for example), while ForEach is an internal iterator of the object being sorted - Akina

3 answers 3

If you use regular arrays with a for-each loop, then at compile time it will be converted to a regular for loop with an integer counter-variable. But if you use for-each with collections, then at compile time it will be converted to for with an iterator (something like for(Iterator it = myColl.iterator(); it.hasNext(); ) ).

    On the contrary, everything that is implemented on foreach can be implemented on for . The fact is that for the object the interface should be implemented Iterable , for example, how do you derive even numbers from 2 to 16?

     for(int i = 2; i<=16; i+=2) System.out.println(i); 

    but through foreach it is necessary to take a steam bath.

    This fragment:

     List<String> list = Arrays.asList("1", "2", "3"); for (String str : list) { System.out.println(str); } 

    Equivalent to writing through regular for is this:

     List<String> list = Arrays.asList("1", "2", "3"); for(int i = 0; i<list.size(); ++i){ System.out.println(list.get(i)); } 

    The @Bakuard answer is not quite correct, the compiler translates foreach into this bytecode:

     List<String> list = Arrays.asList("1", "2", "3"); Iterator var4 = list.iterator(); while(var4.hasNext()) { String str = (String)var4.next(); System.out.println(str); } 
    • Komdosh, but could you throw a link where you can read about what the compiler turns for-each for arrays. Or indicate the source of this information. I will be grateful to you. - Bakuard 3:06 pm
    • @Bakuard I compiled and opened file.class, if you compile jar and your Windows can change the extension in zip and open the archive, find the compiled file there and open with a notebook - Komdosh
    • @Bakuard language specification - JLS §14.14.2 - Sergey Gornostaev

    Prefer the use of for-each to the for loop.

    There is a book - Effective Java Programming, in the 2nd edition this is the 46th article about the advantages of the for-each cycle before for with various examples.

    https://vk.com/doc20299052_453037470?hash=65bcfc6f8cc717c341&dl=d313b5ec8243c42f0e

    The for-each loop has a huge advantage over for because there are some points in programming where it is quite appropriate and easy to use, for example: nested iteration of several collections, simple iteration of objects that implement Iterable, also some things written with using for-each is easier to understand due to the visual simplicity of the syntax.

    However, there are three conditions under which the for-each loop will be useless; this is when an explicit iterator or array index is needed.

    If you need, I can drop in a couple of examples with differences in usage.

    • Let's go, I even ran through the book, please write the page please - user331073 pm
    • @MikeMclaren, 290 pages in the book, bottom left on the page number, there everything is shown with pretty accurate examples, in the browser 311 (and in PDF format) - Alex Tremasov