As was noted by Denis Bueno, this code works for any object that implements the. Also, if the right-hand side of the for : idiom is an array rather than an Iterable object, the internal code uses an int index counter and checks against array. The construct for each is also valid for arrays. Iterable -- it doesn't have to be a list, or some collection from java. Even your own types, therefore, can be used with this syntax. Therefore, when reading each element, one by one and in order, a foreach should always be chosen over an iterator, as it is more convenient and concise. For example, attempting to delete an element while using a foreach can will. It is less precise, but it is useful for education. A foreach for each loop in java syntax is: for type obj:array {. Use the original for loop for that. Iterable The Iterable interface has only one method: Iterator iterator. This works on objects of type Collection because the Collection interface extends Iterable. We can use them as: someList. This avoids potential off-by-one errors and makes code simpler to read. So the concept of a foreach loop describes that the loop does not use any explicit counter which means that there is no need of using for each loop in java to traverse in the list thus it saves user from off-by-one error. To describe the general concept of this off-by-one error, let us take an example of a loop to traverse in a list using indexes. So to avoid this off-by-one error the concept of a foreach loop is used. There may be other advantages too, but this is what I think is the main concept and advantage of using a foreach loop. The new for-loop is easier to read and removes the need for a separate iterator, but is only really usable in read-only iteration passes. It adds beauty to your code by removing all the basic looping clutter. It gives a clean look to your code, justified below. Remember that, your collection should implement Iterator; otherwise you can't use it with for-each. You don't have to worry about initializing the iterator or initializing the loop counter and terminating it where there is scope for errors. For example, for your someList you can do: someList. As so many good answers said, an object must implement the Iterable interface if it wants to use a for-each loop. I'll post a simple example and try to explain in a different way how a for-each loop works. That may explain why object, which doesn't implement the Iterable interface, will throw an Exception when it tries to use the for-each loop. In Java 8, they introduced forEach. Using it List, Maps can be looped. This idiom is implicit as it truly backed by an Iterator. The Iterator is programmed by the programmer and often uses an integer index or a node depending on the data structure to keep track of its position. The aka enhanced for loop is a simplified version of a for loop. The advantage is that there is less code to write and less variables to manage. The downside is that you have no control over the step value and no access to the loop index inside the loop body. They are best used when the step value is a simple increment of 1 and when you only need access to the current loop element. For example, if you need to loop over every element in an array or Collection without peeking ahead or behind the current element. There is no loop initialization, no boolean condition and the step value is implicit and is a simple increment. This is why they are considered so much simpler than regular for loops. So the enhanced for loop declaration states: loop over intArray and store the current array int value in the currentValue variable. Output: 1 3 5 7 9 Example — String Array We can use the for-each loop to iterate over an array of strings. The loop declaration states: loop over myStrings String array and store the current String value in the currentString variable. Output: alpha beta gamma delta Example — Set The enhanced for loop can also be used to iterate over a java. Notice that since this is a Set, duplicate String values are not stored. Output: alpha delta beta gamma Source: protected by Jul 26 '14 at 7:40 Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 on this site the. Would you like to answer one of these instead. Not the answer you're looking for. Browse other questions tagged or.