Java For-Each Loop

A Java for-each loop, also called an enhanced for loop, runs once for each value in an array or collection. It is useful when you want to read every item without manually creating an index variable.

The loop gives you one element at a time. You use that temporary element variable inside the loop body.

Basic Syntax

A for-each loop uses this pattern: for (type variable : arrayOrCollection). The type must match the kind of values stored in the array or collection.

public class Main {
    public static void main(String[] args) {
        String[] languages = {"Java", "Python", "C++"};

        for (String language : languages) {
            System.out.println(language);
        }
    }
}

Output:

Java
Python
C++

How It Works

In the example, languages is an array of String values. On the first loop run, language holds "Java". On the second run, it holds "Python". On the third run, it holds "C++".

When there are no more elements, the loop stops automatically. You do not write a condition like i < languages.length, and you do not update an index with i++.

Looping Through Numbers

For-each loops work with primitive arrays too. The element variable should use the same primitive type as the array values.

public class Main {
    public static void main(String[] args) {
        int[] scores = {82, 91, 76};
        int total = 0;

        for (int score : scores) {
            total += score;
        }

        System.out.println("Total: " + total);
    }
}

Output:

Total: 249

Here, score receives one number from the scores array each time through the loop. The loop adds each score to total.

For-Each With ArrayList

You can also use a for-each loop with many Java collections, such as ArrayList. This makes it a common choice when you want to process every stored value.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Maya");
        names.add("Leo");
        names.add("Nora");

        for (String name : names) {
            System.out.println("Hello, " + name);
        }
    }
}

Output:

Hello, Maya
Hello, Leo
Hello, Nora

When To Use It

Use a for-each loop when you want each element and do not need its position. It is shorter and often clearer than a regular for loop for simple reading tasks.

Use a regular for loop when you need the index, need to skip positions, need to loop backward, or need to change array elements by position.

Changing Values

The element variable is a copy of the current value for primitive arrays. Assigning a new value to it does not change the original array element.

public class Main {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3};

        for (int number : numbers) {
            number = number * 10;
        }

        for (int number : numbers) {
            System.out.println(number);
        }
    }
}

Output:

1
2
3

The first loop changes only the temporary variable number. The array still contains 1, 2, and 3.

Common Mistakes

  • Trying to use a for-each loop when you need the index of the current element.
  • Expecting assignment to the loop variable to update a primitive array element.
  • Using the wrong element type, such as int for a String[].
  • Modifying a collection’s structure while looping through it, which can cause runtime errors.

Takeaway: a Java for-each loop is the cleanest choice when you need to visit every element in an array or collection from start to finish.