Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

The difference between a Java for loop and an Iterator is that they are not competing constructs of the same kind: a for loop is a language statement, while Iterator is an API interface for sequential traversal. Use enhanced for when you simply need each value, an explicit iterator when you need iterator-specific control such as supported removal, and a basic for when the index or numeric step matters.

What are the three ways to traverse values in Java?

Basic for: control the index and step

A basic for statement puts initialization, a boolean continuation condition, and an update expression in its header. It is useful when the index matters, the traversal uses a custom numeric step, or code is working directly with array positions. The programmer must ensure the bounds and update are correct.

for (int i = 0; i < values.length; i++) {
    process(values[i]);
}

Enhanced for: visit each value

The enhanced form, often called for-each, assigns each successive value to the loop variable. It accepts an array or an expression whose type is a subtype of Iterable. It is usually the clearest choice when the operation is simply to process every value.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
for (String value : values) {
    process(value);
}

For an Iterable, the Java Language Specification describes enhanced-for in terms of obtaining an Iterator and repeatedly checking for and retrieving the next value. The loop hides that traversal bookkeeping. Arrays are supported through separate array-specific semantics. The Java SE 25 Language Specification, §14.14.2 specifies that the expression must be an array type or a subtype of raw Iterable; a bare Iterator is not sufficient.

Explicit Iterator: control traversal through its API

An Iterator represents sequential traversal through an underlying collection. Its core operations are hasNext() and next(). Write the traversal explicitly when you need to use an iterator operation or when an iterator has already been provided to your code.

Iterator<String> it = values.iterator();
while (it.hasNext()) {
    String value = it.next();
    process(value);
}

The Java SE 21 Iterable API documents the relationship between iterable values and iterators; the Java SE 26 Iterator API defines the iterator operations and their contracts.

When should you use each form?

Need Prefer Why
Visit every element of an array or Iterable Enhanced for Successive values are assigned automatically, with less traversal code.
Remove the current element during traversal Explicit Iterator Iterator.remove() is the iterator-mediated removal operation, when supported.
Use an index or control a numeric step Basic for Initialization, continuation condition, and update are explicit.
Traverse an Iterator already supplied to the code Explicit Iterator It exposes hasNext() and next(); enhanced-for requires an array or Iterable expression.

How do you remove elements safely while traversing?

When an iterator supports removal, call remove() after next() to remove the last element returned. It may be called only once for each next(), and the operation is optional: an implementation may throw UnsupportedOperationException.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Iterator<String> it = values.iterator();
while (it.hasNext()) {
    String value = it.next();
    if (shouldRemove(value)) {
        it.remove();
    }
}

Do not assume every collection’s iterator permits removal. Also avoid modifying the underlying collection directly while traversing it: the Iterator contract says behavior is unspecified if the collection is modified by a means other than that iterator’s remove(), unless the implementation documents a concurrent-modification policy. A particular exception or fail-fast response is not a universal guarantee, nor a substitute for synchronization.

Common misconceptions about loops and iterators

  • “Iterator is another spelling of for.” A basic or enhanced for is a language statement; Iterator is an API interface. Enhanced-for over an Iterable is specified using iterator-style traversal.
  • “Enhanced-for works on any iterator.” Its expression must be an array or a subtype of Iterable. A bare Iterator does not satisfy that requirement.
  • “Enhanced-for only works on collections.” Arrays are valid enhanced-for expressions too.
  • “Every iterator can remove elements.” Removal is optional and may be unsupported.
  • “External modification always causes one predictable exception.” The iterator contract leaves behavior unspecified unless an implementation defines its policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Is a for loop faster than an iterator?

There is no universal speed conclusion established by the Java language specification or the cited API contracts. They define what the constructs do, not comparative runtime performance. Choose based on clarity and the control your task requires; a speed claim needs measurements for a specified workload, Java runtime, and collection.

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.