HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › The enhanced for loop

What javac does

The enhanced for loop

An index loop over arrays, an Iterator over everything else.

The enhanced for loop compiles two different ways. Over an array it becomes an index loop, with hidden copies of the array reference and its length. Over anything Iterable it becomes calls to iterator(), hasNext() and next().

The source

// The enhanced for loop: an index loop over arrays, an Iterator over everything else.
import java.util.List;

public class ForEach {
    static int sum(int[] values) {
        int total = 0;
        for (int v : values) {
            total += v;
        }
        return total;
    }

    static int totalLength(List<String> words) {
        int total = 0;
        for (String w : words) {
            total += w.length();
        }
        return total;
    }
}

Compiled by javac 25 with -g and --release 8 to 25. Where releases produce different bytecode, each version is shown.

What to look for

  • In sum(int[]), slots 2, 3 and 4 hold the copied array, its length and the index: local variables you never declared, which have no names in the LocalVariableTable.
  • arraylength is read once, before the loop, so assigning a different array to the variable inside the loop does not change what is iterated.
  • Over a List<String>, next() returns Object, so javac adds checkcast String: generics are erased.
  • Both loops test at the top and jump back with goto.

ForEach.class

static int sum(int[] values)

javac --release 8 to 25 (the same bytecode for every release)

6int total = 0;
0iconst_0
1istore_1 // total
7for (int v : values) {
2aload_0 // values
3astore_2
4aload_2
5arraylength
6istore_3
7iconst_0
8istore 4
10iload 4
12iload_3
13if_icmpge 33
16aload_2
17iload 4
19iaload
20istore 5// v
8total += v;
22iload_1 // total
23iload 5// v
25iadd
26istore_1 // total
7for (int v : values) {
27iinc 4, 1
30goto 10
10return total;
33iload_1 // total
34ireturn

Open in the viewer

static int totalLength(List words)

javac --release 8 to 25 (the same bytecode for every release)

14int total = 0;
0iconst_0
1istore_1 // total
15for (String w : words) {
2aload_0 // words
3invokeinterface #7, 1// List.iterator() : Iterator
8astore_2
9aload_2
10invokeinterface #13, 1// Iterator.hasNext() : boolean
15ifeq 38
18aload_2
19invokeinterface #19, 1// Iterator.next() : Object
24checkcast #23// String
27astore_3 // w
16total += w.length();
28iload_1 // total
29aload_3 // w
30invokevirtual #25// String.length() : int
33iadd
34istore_1 // total
17}
35goto 9
18return total;
38iload_1 // total
39ireturn

Open in the viewer

← AutoboxingAll topicstry-with-resources →

By the author of this site

Reading bytecode is half the story; running it is the other half. You execute exactly this kind of code in Build a JVM in Rust: Class files, an interpreter, objects and invokedynamic from scratch, on the real JDK class library.

Read Chapter 1 free

Java and the JVM are trademarks of Oracle; this site is not affiliated with Oracle. Field names follow the Java Virtual Machine Specification; the explanations are our own.

Sister sites: What's in my DB · LLM Anatomy · Distribution Atlas · Notation Decoder · uresk.net

Made by Spencer Uresk · About · Files you open stay on your device.