If you're looping through an array, it shouldn't matter - the enhanced for loop uses array accesses anyway.

For example, consider this code:

public static void main(String[] args)
{
    for (String x : args)
    {
        System.out.println(x);
    }
}

When decompiled with javap -c Test we get (for the main method):

public static void main(java.lang.String[]);
  Code:
   0:   aload_0
   1:   astore_1
   2:   aload_1
   3:   arraylength
   4:   istore_2
   5:   iconst_0
   6:   istore_3
   7:   iload_3
   8:   iload_2
   9:   if_icmpge   31
   12:  aload_1
   13:  iload_3
   14:  aaload
   15:  astore  4
   17:  getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   20:  aload   4
   22:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   25:  iinc    3, 1
   28:  goto    7
   31:  return

Now change it to use an explicit array access:

public static void main(String[] args)
{
    for (int i = 0; i < args.length; i++)
    {
        System.out.println(args[i]);
    }
}

This decompiles to:

public static void main(java.lang.String[]);
  Code:
   0:   iconst_0
   1:   istore_1
   2:   iload_1
   3:   aload_0
   4:   arraylength
   5:   if_icmpge   23
   8:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   11:  aload_0
   12:  iload_1
   13:  aaload
   14:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   17:  iinc    1, 1
   20:  goto    2
   23:  return

There's a bit more setup code in the enhanced for loop, but they're basically doing the same thing. No iterators are involved. Furthermore, I'd expect them to get JITted to even more similar code.

Suggestion: if you really think it might make a significant difference (which it would only ever do if the body of the loop is absolutely miniscule) then you should benchmark it with your real application. That's the only situation which matters.

Answer from Jon Skeet on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arrays_loop.asp
Java Loop Through an Array
There is also a "for-each" loop, which is used exclusively to loop through elements in an array (or other data structures):
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ iterating-arrays-java
Java - Loop Through an Array - GeeksforGeeks
December 2, 2024 - While loop is considered effective when the increment of the value depends upon some of the condition. ... // Java program to iterate over an array // using while loop import java.io.*; class Main { public static void main(String args[]) { // taking an array int a[] = { 1, 2, 3, 4, 5 }; int i = 0; // Iterating over an array // using while loop while (i < a.length) { // accessing each element of array System.out.print(a[i] + " "); i++; } } }
Top answer
1 of 6
69

If you're looping through an array, it shouldn't matter - the enhanced for loop uses array accesses anyway.

For example, consider this code:

public static void main(String[] args)
{
    for (String x : args)
    {
        System.out.println(x);
    }
}

When decompiled with javap -c Test we get (for the main method):

public static void main(java.lang.String[]);
  Code:
   0:   aload_0
   1:   astore_1
   2:   aload_1
   3:   arraylength
   4:   istore_2
   5:   iconst_0
   6:   istore_3
   7:   iload_3
   8:   iload_2
   9:   if_icmpge   31
   12:  aload_1
   13:  iload_3
   14:  aaload
   15:  astore  4
   17:  getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   20:  aload   4
   22:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   25:  iinc    3, 1
   28:  goto    7
   31:  return

Now change it to use an explicit array access:

public static void main(String[] args)
{
    for (int i = 0; i < args.length; i++)
    {
        System.out.println(args[i]);
    }
}

This decompiles to:

public static void main(java.lang.String[]);
  Code:
   0:   iconst_0
   1:   istore_1
   2:   iload_1
   3:   aload_0
   4:   arraylength
   5:   if_icmpge   23
   8:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   11:  aload_0
   12:  iload_1
   13:  aaload
   14:  invokevirtual   #3; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   17:  iinc    1, 1
   20:  goto    2
   23:  return

There's a bit more setup code in the enhanced for loop, but they're basically doing the same thing. No iterators are involved. Furthermore, I'd expect them to get JITted to even more similar code.

Suggestion: if you really think it might make a significant difference (which it would only ever do if the body of the loop is absolutely miniscule) then you should benchmark it with your real application. That's the only situation which matters.

2 of 6
22

This falls squarely in the arena of micro-optimization. It really doesn't matter. Stylistically I always prefer the second because it's more concise, unless you need the loop counter for something else. And that's far more important than this kind of micro-optimization: readability.

That being said, For an ArrayList there won't be much difference but a LinkedList will be much more efficient with the second.

๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ java-and-array-iteration-what-beginners-need-to-know-22a44dd32afb
Java and Array Iteration โ€” What Beginners Need to Know
June 17, 2024 - In this example, the loop starts with the index i initialized to 0. The condition i < numbers.length && numbers[i] != target makes sure that the loop continues as long as i is less than the length of the array and the current element is not equal to the target. The index i is incremented by 1 in each iteration until the target is found or the end of the array is reached. Beyond the basic iteration methods, Java offers more advanced techniques to iterate over arrays.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ csjava โ€บ Unit7-Arrays โ€บ topic-7-2-traversing-arrays.html
7.2. Traversing Arrays with For Loops โ€” CS Java
First trace through it on paper keeping track of the array and the index variable. Then, run it to see if you were right. You can also follow it in the visualizer by clicking on the Show Code Lens button. We can use iteration with a for loop to visit each element of an array.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2016 โ€บ 02 โ€บ how-to-loop-through-array-in-java-with.html
How to loop through an Array in Java? Example Tutorial
This technique will work for all kind of array, except the multi-dimensional array. In order to loop through the two-dimensional array, you need to use nested loops as shown in the example given here.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ enhanced-for-loop
Java for-each Loop (With Examples)
// print array elements class Main { public static void main(String[] args) { // create an array int[] numbers = {3, 9, 5, -5}; // for each loop for (int number: numbers) { System.out.println(number); } } } ... Here, we have used the for-each loop to print each element of the numbers array ...
Find elsewhere
Top answer
1 of 5
3

The better question might be: Why wouldn't you want to use a FOR loop to iterate through an array? There are many ways to iterate through an Array or a collection and there is no law that states you have to use the FOR loop. In a lot of cases, it's simply the best to use for speed, ease of use, and readability. And yet, in other cases it is not:

The Array:

int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Display Array with the typical for loop:

for (int i = 0; i < array.length; i++) {
    System.out.println(array[i]);
}

Display Array with the enhanced for loop:

for(Integer num : array) {
    System.out.println(num);
}

Display Array with the do/while loop:

int i = 0;
do {
    System.out.println(array[i++]);
} while (i < array.length);

Display Array with the while loop:

int j = 0;
while (j < array.length) {
    System.out.println(array[j++]);
}

Display Array through Recursive Iteration:

iterateArray(array, 0);  // 0 is the start index.


// The 'iterateArray()' method:
private static int iterateArray(int[] array, int index) {
    System.out.println(array[index]);
    index++; 
    if (index == array.length) {
        return 0;
    }
    return iterateArray(array,index);
}

Display Array using Arrays.stream() (Java8+):

Arrays.stream(array).forEach(e->System.out.print(e + System.lineSeparator())); 

Display Array using IntStream (Java8+):

IntStream.range(0, array.length).mapToObj(index -> array[index]).forEach(System.out::println);

Choose your desired weapon....

2 of 5
1
Considering you have an array like : 
int[] array = {1,2,4,5,6};

You can use stream to iterate over it, apart from printing you can perform lot many thing over this array.

Arrays.stream(array).forEach(System.out::println);

Similarly you can do lot many action over collections as well:

List<String> myList = new ArrayList<>(); List
myList.add("A");
myList.add("B");
    

Stream.of(myList).forEach(System.out::println);

myList.forEach(System.out::println);

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ what-are-the-different-ways-to-iterate-over-an-array-in-java
What are the different ways to iterate over an array in Java?
July 2, 2020 - import java.util.Arrays; public class IteratingArray { public static void main(String args[]) { //Creating an array int myArray[] = new int[7]; //Populating the array myArray[0] = 1254; myArray[1] = 1458; myArray[2] = 5687; myArray[3] = 1457; myArray[4] = 4554; myArray[5] = 5445; myArray[6] = 7524; //Printing Contents using for each loop System.out.println("Contents of the array: "); for (int element: myArray) { System.out.println(element); } } }
๐ŸŒ
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
To set up a for-each loop, use for (type variable : arrayname) where the type is the type for elements in the array, and read it as โ€œfor each variable value in arraynameโ€. for (type item: array) { //statements using item; } See the examples below in Java that loop through an int and a String ...
๐ŸŒ
CodingBat
codingbat.com โ€บ doc โ€บ java-array-loops.html
CodingBat Java Arrays and Loops
In contrast, Java Lists can grow and shrink over time -- this is a big feature that Lists have that arrays do not. The length of an array can be accessed as a special ".length" attribute. For example, with the above "values" array, we can access the size of the array as "values.length". It is common to use a 0...length-1 for-loop to iterate over all the elements in array:
๐ŸŒ
Instructables
instructables.com โ€บ design โ€บ software
How to Use a While Loop to Iterate an Array in Java : 9 Steps - Instructables
July 22, 2022 - How to Use a While Loop to Iterate an Array in Java: Today I will be showing you how to use Java to create a While loop that can be used to iterate through a list of numbers or words. This concept is for entry-level programmers and anyone who wants to get a quick brush-up on Java Loops and arrays.
๐ŸŒ
Quora
quora.com โ€บ Can-you-iterate-through-an-array-in-Java-using-only-one-loop
Can you iterate through an array in Java using only one loop? - Quora
Answer (1 of 2): Yes, you can iterate through an array in Java using only one loop. You can use a [code ]for[/code] loop to achieve this. [code]int[] numbers = {1, 2, 3, 4, 5}; for (int i = 0; i
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-iterate-over-arrays-using-for-and-foreach-loop
Java Program to Iterate Over Arrays Using for and for-each Loop - GeeksforGeeks
July 23, 2025 - To access and manipulate elements, we can use iteration techniques such as the for loop and for-each loop (also known as the enhanced for loop). In the below example, we will demonstrate how to iterate through an array using both ...
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ java tutorial โ€บ java array iterator
Java Array Iterator | How Does an Array Iterator Works in Java?
March 28, 2023 - As we discussed above, array elements can be iterated in 3 ways, so based on the looping functionality array can iterate its elements. ... public class Main { public static void main(String[] args) { //defining static array with 10 elements int arrayNumbers[] = {1,2,3,4,5,6,7,8,9,10}; //for loop for(int i=0;i<arrayNumbers.length;i++) { //displaying array index and its corresponding values System.out.println("arrayNumbers["+i+"]"+" index value :"+arrayNumbers[i]); } } } ... import java.util.Scanner; public class Main { public static void main(String[] args) { // Scanner for taking input from us
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Java67
java67.com โ€บ 2013 โ€บ 08 โ€บ how-to-iterate-over-array-in-java-15.html
How to iterate over an Array in Java using foreach loop Example Tutorial | Java67
Java 1.5 foreach loop provides an elegant way to iterate over array in Java. In this programming tutorial, we will learn how to loop over String array in Java by using foreach loop.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-iterate-array-elements-efficiently-418988
How to iterate array elements efficiently | LabEx
This tutorial will explore various methods and best practices for traversing arrays, helping developers optimize their iteration strategies and improve overall code efficiency. Arrays are fundamental data structures in Java that store multiple elements of the same type in a contiguous memory location. Understanding how to iterate through array elements is crucial for effective programming.