Enhanced for loop (for-each)
The advanced for loop, also known as the for-each loop, was introduced in Java 5. The for-each loop in Java is used to iterate data information in a small and easily readable order over collections, arrays, and other iterable class objects. This eliminates the need to use a for-each loop iterator in Java programs or to access and retrieve array list data elements in a clear order from their index storage locations.

Improved syntax for a For Loop in Java.
for (Type element : collection) {
// here we can Access the array list collection element
}
For Loop in Java element.
- Here, the for-each loop type is a data type of the elements in the data collection. For example, int, String, double, etc., the data type is defined.
- In this, the for-each loop element is a program variable that displays the current element in the for-each loop iteration process.
- The for-each loop program collection is the array or collection that Java users want to iterate over using the for-each loop concept. Such as ArrayList, HashSet, array, etc.
Advanced For Loop Features in Java.
- Simple syntax – The for-each loop in Java programs simplifies the sequence of loop variable elements iterating over array and collection data type elements.
- No index management – The Java programmer does not need to manage or control the declared variable element index storage location in the program or use the iterator in an explicit order.
- Read-only – The for-each loop in Java programs allows access to each element of a user-defined collection data element in a read-only order. This means it does not support removing or modifying array elements during the iteration process.
Example of using an enhanced For loop in Java.
Iterating over a user-defined array of data element values using a for-each loop.
public class ForEachLoopIllustration {
public static void main(String[] args) {
// Here we declare an array of numeric data elements
int[] numeric = {10, 14, 19, 22, 27, 30, 33};
// Here we iterate over the array using the enhanced for loop concept
for (int number : numeric) {
System.out.println(number);
}
}
}
Here in the for-each loop example.
- Here, the enhanced for-each loop iterates from start to end over each element’s value data in the numeric array, and prints each element of the array collection as it is accessed.
Iterating over an ArrayList collection in a Java program using a for-each loop.
import java.util.ArrayList;
public class ForEachLoopIllustration {
public static void main(String[] args) {
// Here we create a collection of ArrayList Strings data type.
ArrayList<String> employee = new ArrayList<>();
employee.add(“Bhavishi Deora”);
employee.add(“Siddhi Deora”);
employee.add(“Harry Deora”);
employee.add(“Amit Saxena”);
employee.add(“Vinay Sharma”);
// Here we iterate over the ArrayList using the enhanced for loop concept
for (String employees : employee) {
System.out.println(employees);
}
}
}
ArrayList collection in a Java program explanation.
- In this program, an advanced for loop is used to iterate from start to end over the ArrayList employees data type element, printing the data and information for each employee in the current arraylist element to the console screen.
Important features of an advanced for loop in a Java program.
- Read-only access – An advanced for loop in a Java program provides read-only access to data element values. This means that Java users cannot modify or remove variable elements from a collection data type while iterating over the loop variable values in the current program. For example, Java users cannot use an advanced for loop to modify the values of elements in a list. Similarly, Java users can modify the fields of a class object’s elements if they are defined as mutable in the current program.
- No index or iterator – An advanced for loop in a Java program eliminates the need to manually manage index storage locations or use iterators. This helps Java developers read program code more easily and avoid mistakes in the current program.
- Array and Iterable – The Java Advanced For Loop can work with arrays and any class object that supports implementing the Iterable interface in a program, such as ArrayList, HashSet, etc. Furthermore, the Advanced For Loop cannot be used with primitive data types that do not implement the Iterable interface. For example, an int[] array block can be used, but not the int data type directly.
- Efficiency – The performance of an Advanced For Loop in Java is generally similar to using an iterator or index-based for loop to read a new element from a collection data type.
Advanced for loop usage summary in Java programming.
- Simple iteration – When the Java user needs to iterate over all element value locations in a collection or array data type and does not need to modify the data collection during the for loop iteration process.
- Code readability – The Java advanced for loop is more clear and smaller in size than the traditional for loop, making it a better choice for simple Java program iteration tasks.
- Immutable access – If the Java user only needs to access each array collection element, and does not need to modify each array collection element or their order.
