Enhanced for loop:

for (String element : array) {

    // rest of code handling current element
}

Traditional for loop equivalent:

for (int i=0; i < array.length; i++) {
    String element = array[i]; 

    // rest of code handling current element
}

Take a look at these forums: https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with

http://www.java-tips.org/java-se-tips/java.lang/the-enhanced-for-loop.html

Answer from user1920811 on Stack Overflow
🌐
Programiz
programiz.com › java-programming › enhanced-for-loop
Java for-each Loop (With Examples)
In Java, the for-each loop is used to iterate through elements of arrays and collections (like ArrayList). It is also known as the enhanced for loop.
Discussions

Can someone explain the enhanced for-loop ? as simple as possible ?
Sure. Say you have an array list of strings (ArrayList list), and you wanted to print out each of the elements in the list. You could do it one of two ways: for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } or method 2: for (String s : list) { System.out.println(s); } This second function is the enhanced for loop. So basically if you're iterating over a list or set, you don't have to write out the full old for loop syntax, you can just write the much more readable enhanced for-loop syntax. Its much more readable because the bounds of the for-loop are very clear (you're iterating over each element of the for loop from start to end) and you immediately have access to each element without calling .get(index). The downsides of the enhanced for-loop is that you don't have access to the index you're at without explicitly keeping track of it, and that it only works on objects that extend the Collections interface, so really only lists and sets. So no enhanced for loops on hashmaps or iterators. More on reddit.com
🌐 r/learnjava
10
5
July 20, 2015
What's the purpose of using a for-each/enhanced for-loop?
It makes the code easier to read, allows the use of lambdas (in the case of stream()) and eliminates the need for an index variable and a get() operation required by the classic for loop. If, say, you wanted to print the contents of an ArrayList list using a classic for loop, it would look like this: for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } Using an enhanced for loop, it would look like this: for(String s: list) { System.out.println(s); } There is now no need to use an index variable or a get() operation. More on reddit.com
🌐 r/learnjava
9
14
April 26, 2019
Uses of Enhanced For Loop in Java?
It's not used just to display arrays. It can be used for a variety of things (finding max in an array, finding number of elements that satisfy a criteria, etc). You sound like you are preparing for an exam or something. If so, the teacher using the phrase "enhanced" seems wrong. Should be called a foreach loop (even though each does not appear in the syntax). More on reddit.com
🌐 r/learnprogramming
10
1
March 24, 2022
Why does the "for" loop work so different from other languages like Java or C++ .
Language designer decision. More of a for each than a traditional for but still fundamentally a while loop with some of the work done for you. More on reddit.com
🌐 r/learnpython
70
169
February 16, 2022
🌐
Zero To Mastery
zerotomastery.io › blog › enhanced-for-loop-java
How To Use Enhanced For Loops In Java (aka 'foreach') | Zero To Mastery
January 26, 2024 - Java 5 introduced the enhanced for loop in Java. In this guide, I break down what it is, it's differences to the for loop, when best to use either, and more!
🌐
freeCodeCamp
freecodecamp.org › news › enhanced-for-loops-in-java-how-to-use-foreach-loops-on-arrays
Enhanced For Loops in Java – How to Use ForEach Loops on Arrays
February 17, 2023 - You can use enhanced loops in Java to achieve the same results as a for loop. An enhanced loop is also known as a for-each loop in Java. Enhanced loops simplify the way you create for loops. They are mostly used to iterate through an array or ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-for-loop-and-enhanced-for-loop-in-java
Java for loop vs Enhanced for loop - GeeksforGeeks
July 23, 2025 - The enhanced for loop, also known as the for-each loop, is a streamlined way to iterate over collections and arrays. It eliminates the need for managing the loop's counter or bounds manually.
🌐
CodeGym
codegym.cc › java blog › java collections › enhanced for loop in java
Enhanced for loop in Java
February 13, 2025 - We don't have to keep track of an index or worry about the length of the array. The enhanced for loop takes care of all of this for us. We can also use the enhanced for loop to iterate over a collection. Let's take a look at an example of iterating over an ArrayList of Strings. // Here is the example of an enhanced for loop to iterate over a collection import java.util.ArrayList; public class EnhancedForLoop { public static void main(String[] args) { // use the enhanced for loop to iterate over the `names` ArrayList ArrayList<String> names = new ArrayList<String>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); for (String name : names) { System.out.println(name); } } }
Find elsewhere
🌐
Oracle
docs.oracle.com › cd › E19253-01 › 817-7970 › features-forloop › index.html
Enhanced for Loop
Documentation Home > JDK for Solaris Developer's Guide > New Features and Enhancements > Java Language Features > Enhanced for Loop ... This new language construct eliminates the drudgery and error-proneness of iterators and index variables when iterating over collections and arrays.
🌐
Runestone Academy
runestone.academy › ns › books › published › csjava › Unit7-Arrays › topic-7-3-arrays-with-foreach.html
7.3. Enhanced For-Loop (For-Each) for Arrays — CS Java
There is a special kind of loop that can be used with arrays called an enhanced for loop or a for each loop. This loop is much easier to write because it does not involve an index variable or the use of the []. It just sets up a variable that is set to each value in the array successively.
🌐
Quora
quora.com › How-and-when-should-we-use-an-enhanced-for-loop-arrays-in-Java
How and when should we use an enhanced for loop (arrays in Java)? - Quora
The enhanced for loop offers simple syntax to iterate through a collection of values-an array, ArrayList, or other classes from Java's Collection framework that store a collection of values.
🌐
Blogger
javarevisited.blogspot.com › 2016 › 02 › how-does-enhanced-for-loop-works-in-java.html
How foreach or Enhanced for loop works in Java? Example
July 21, 2023 - The enhanced loop provides the cleanest way to loop through an array or collection in Java, as you don't need to keep track of the counter or you don't need to call the hasNext() method to check whether there are more elements left.
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit6-Arrays › topic-6-3-arrays-with-foreach.html
6.3. Enhanced For-Loop (For-Each) for Arrays — CSAwesome v1
Would that work with an enhanced for-each loop? Unfortunately not! Because only the variable in the loop changes, not the real array values. We would need an indexed loop to modify array elements. Try it in the Active Code below or in the Java visualizer by clicking the CodeLens button and ...
🌐
JetBrains
jetbrains.com › help › inspectopedia › ForLoopReplaceableByForEach.html
'for' loop can be replaced with enhanced for loop | Inspectopedia Documentation
December 3, 2025 - Reports for loops that iterate over collections or arrays, and can be automatically replaced with an enhanced for loop (foreach iteration syntax). ... for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) { String item = ...
🌐
Minich
minich.com › education › wyo › java › lecture_notes › enhanced_for_loops.php
Enhanced For Loops
For example, the task of adding one to each integer in an array with an enhanced for loop would cause an error. int[] scores = {99, 75, 82}; for (int value : scores) { value++; } The loop above would not modify scores so that it contains {100, 76, 83}. The array would still contain the values {99, 75, 82}. To be safe, you should only perform tasks with an enhanced for loop that need to access and "read" elements of an array rather than modifying the elements in any way. Objective #2: Use nested "enhanced" for loops with two-dimensional arrays.
🌐
Blogger
javarevisited.blogspot.com › 2017 › 01 › difference-between-for-loop-and-enhanced-forlop-in-java.html
Difference between for loop and Enhanced for loop in Java? Example Tutorial
All the differences, you will learn in this article, stems from the very fact that traditional for loop gives more control than enhanced for loop but on the other hand enhanced or advanced for loop gives more convenience. So, the choice is up to you, if you need more control then traditional for loop is ideal and if you don't need that level of control, just opt for convenience by using enhanced for loop as shown here. And, If you are new to the Java world then I also recommend you go through The Complete Java MasterClass on Udemy to learn Java in a better and more structured way.
🌐
Javatpoint
javatpoint.com › for-each-loop
Java For-each Loop | Enhanced For Loop - javatpoint
Java For-each loop | Java Enhanced For Loop: The for-each loop introduced in Java5. It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable.
🌐
FavTutor
favtutor.com › blogs › java-enhanced-for-loop
Enhanced For Loop Java (with Codes)
December 1, 2023 - The enhanced for loop, also known as the "for-each" loop, provides a concise and intuitive way to traverse arrays, collections, and other iterable objects in Java. Unlike traditional loops, the for loop abstracts away the complexity of managing ...
🌐
Java Community Process
jcp.org › aboutJava › communityprocess › jsr › tiger › enhanced-for.html
An enhanced for loop for the Java™ Programming Language
Note that the cast may generate a warning or error: if Expression has a raw type, then an unchecked assignment warning will be generated, correctly implying that the implicit cast may fail. If Expression has a parameterized type, the statement will generate a compilation error if the type parameter ...
🌐
Xperti
xperti.io › home › understanding the foreach (enhanced for loop) in java
Understanding the ForEach (Enhanced For Loop) in Java
May 5, 2023 - The for-each or enhanced for loop in Java helps developers apply the same conditional barriers with less hassle. Aptly named enhanced for loop, it does precisely what for loop does but takes less effort and time.