Recommended Free Tools
A Java for loop and an Iterator are related, but they are not interchangeable constructs. Use a basic for loop when you need to control an index or step; use enhanced for for straightforward traversal of an array or Iterable; use an explicit Iterator when you need iterator operations such as removing the current element when supported.
What is the difference between a for loop and an iterator in Java?
A basic for loop is a language statement with explicit initialization, a continuation condition, and an update expression. An Iterator is an API interface for sequentially traversing elements, principally through hasNext() and next(). Enhanced for—also called for-each—is another language statement: it hides traversal bookkeeping and works with arrays or values whose type implements Iterable.
As an Amazon Associate I earn from qualifying purchases.
For an Iterable, enhanced-for obtains and uses an iterator as part of its specified behavior. That means a for-each loop is not an alternative to iterator mechanics so much as concise syntax that uses them without exposing the iterator variable. Arrays are also supported by enhanced-for through separate array-specific rules. The Java Language Specification says the expression in an enhanced-for statement must be an array type or a subtype of raw Iterable; otherwise, it is a compile-time error (Java SE 25 Language Specification, §14.14.2).
How the three approaches work
Basic or indexed for loop
The basic form puts the starting state, stopping condition, and update in the loop header. It fits cases where the index matters, you need a custom numeric step, or you are accessing an array by position.
for (int i = 0; i < values.length; i++) {
process(values[i]);
}
You are responsible for the bounds and for updating the index correctly. This is a language construct, not an Iterator API.
Enhanced for loop
Enhanced-for visits successive values with less traversal code. It works with an array or an Iterable, such as a collection that supplies an iterator.
Rank #2
for (String value : values) {
process(value);
}
For an Iterable, the language’s specified translation handles obtaining the iterator, checking whether another element exists, and retrieving it for the loop variable. The Java SE 21 Iterable API describes the interface’s iterator-based traversal contract.
Explicit Iterator
Use an explicit iterator when your code needs direct access to traversal operations or the iterator itself has been supplied to your method.
Iterator<String> it = values.iterator();
while (it.hasNext()) {
String value = it.next();
if (shouldRemove(value)) {
it.remove();
}
}
remove() is optional: an implementation may throw UnsupportedOperationException. When supported, it removes the last element returned by that iterator, and it may be called only once for each call to next(). See the Java SE 26 Iterator API for the contract.
Which should you choose?
| What you need | Prefer | Why |
|---|---|---|
Visit every value in an array or Iterable with minimal traversal code |
Enhanced for |
It assigns successive elements to the loop variable. |
| Remove the current element during traversal | Explicit Iterator |
Iterator.remove() is the iterator-mediated removal operation, if supported. |
| Use an index or control a numeric step | Basic for |
The initialization, condition, and update are explicit. |
Traverse an Iterator that is already available |
Explicit Iterator |
Enhanced-for accepts an array or Iterable, not a bare Iterator. |
Removing elements safely while looping
If you need to remove the element just returned by traversal, call remove() on the explicit iterator, subject to that iterator’s support for removal. Do not assume every collection permits it.
Rank #4
Changing the underlying collection during traversal by any means other than the iterator’s own remove() leaves behavior unspecified under the Iterator contract, unless that implementation documents a concurrent-modification policy. A particular exception—such as a fail-fast exception—is not guaranteed by the general contract, and fail-fast behavior is not a substitute for synchronization.
Common misconceptions
- “Iterator is another spelling of
for.” A for loop is a language statement; an Iterator is an API interface. Enhanced-for over anIterableuses iterator-style traversal behind the scenes. - “Enhanced-for works on any Iterator.” Its expression must be an array or an
Iterable; a bare Iterator does not meet that requirement. - “Enhanced-for is only for collections.” Arrays are valid enhanced-for expressions too.
- “Every Iterator can remove elements.” Removal is optional and may be unsupported.
- “External changes always trigger one predictable exception.” The Iterator contract does not promise that outcome for every implementation.
Does one approach run faster?
The Java language specification and API contracts define what these constructs do; they do not establish that one form is universally faster. Performance depends on the workload, runtime, data structure, and implementation. Without a benchmark for those specific conditions, choose based on correctness and clarity rather than assuming that indexed, enhanced, or explicit-iterator traversal wins on speed.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




