The classic for loop is flexible, but most of the time you just want to walk through every element of an array or list, one at a time. Java 5 added the enhanced for loop, also called the for-each loop, for exactly that case. It strips away the index bookkeeping and lets you focus on what you're doing with each element.
The enhanced for loop reads like English: "for each item in this collection, do the following."
You declare a variable that holds the current element, follow it with a colon, and then name the thing you're iterating over. On each pass, item is set to the next element. The loop ends when there are no more elements.
Here's the contrast with a classic for loop. Both print every product in a cart:
The enhanced version is shorter and harder to get wrong. There is no i = 0 to remember, no i < cartItems.length to type, no cartItems[i] to look up. The loop variable cartItem is the element itself, not an index pointing to it.
The enhanced for loop was introduced in Java 5 (2004). Every modern Java codebase uses it.
The flow is simple: pick the next element, assign it to the loop variable, run the body, repeat.
There's no counter exposed to your code. The loop knows how many elements remain because the array or collection knows its own size, but you never see that number unless you ask.
A more realistic example: summing the prices in a cart.
The loop reads naturally: "for each price in prices, add it to the total."
The enhanced for loop isn't just for arrays. It works on anything that implements the Iterable interface, which includes List, Set, and most of the standard collection types you'll encounter later.
You can think of it like this: if a type can hand out its elements one at a time, the enhanced for loop can walk it.
The body of the loop looks identical to the array version. The same syntax handles arrays, lists, and sets without you having to think about it.
for Loop WinsThe enhanced version is cleaner, but it gives up some power in exchange. There are four situations where you'll want to go back to the classic for loop.
The enhanced for loop hides the index from you. If you need to know whether you're on the first item, the last item, or position i, you don't have access to that number directly.
Say you want to print a numbered list of cart items:
You can fake the index with a separate counter variable inside an enhanced for loop, but at that point the classic version is just clearer.
The enhanced for loop always goes from first to last. There's no "iterate backwards" mode. If you need to walk an array from the end to the beginning, you need a classic for loop with a decrementing counter.
The enhanced for loop iterates one thing at a time. If you have product names in one array and prices in a parallel array, and you want to print them side by side, the enhanced version can't do that on its own. You need an index to look into both.
You can't safely add or remove elements from a list while you're iterating it with an enhanced for loop. If you try, you'll get a ConcurrentModificationException at runtime.
Here's a trap that catches beginners. The loop variable is a copy of the element, not a reference back into the array. Reassigning it doesn't change the array.
What's wrong with this code?
The discount didn't apply. The variable price is a local copy of each array element. When you reassign price, you change the copy, not the array slot it came from. Java passes values, not slots, into the loop variable.
Fix: Use a classic for loop with an index so you can write back into the array.
prices[i] = ... writes back into the array. Now the change sticks.
The same rule applies to primitives in any context: assigning to a copy never affects the original. With objects the story is more nuanced (you can still call methods that mutate the object the reference points to), but reassigning the loop variable itself never updates the array slot.
Here's a slightly bigger example that combines a few ideas. Given a list of product ratings, compute the average rating and count how many are 5-star reviews.
The enhanced for loop handles the traversal. The accumulator variables (sum, fiveStarCount) live outside the loop, get updated inside, and we read their final values afterward. No index needed, because we don't care about position.
10 quizzes