HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › for, while and do-while

What javac does

for, while and do-while

The test at the top and a goto at the bottom, except for do-while.

javac compiles for and while loops with the test at the top: a conditional jump out of the loop, the body, and a goto back to the test. A do-while has its test at the bottom and needs no goto.

The source

// for, while and do-while: the condition at the top, a backward goto at the bottom.
public class Loops {
    static int forLoop(int n) {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += i;
        }
        return sum;
    }

    static int whileLoop(int n) {
        int steps = 0;
        while (n != 1) {
            n = n % 2 == 0 ? n / 2 : 3 * n + 1;
            steps++;
        }
        return steps;
    }

    static int doWhile(int n) {
        int digits = 0;
        do {
            n /= 10;
            digits++;
        } while (n != 0);
        return digits;
    }
}

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 forLoop, if_icmpge leaves the loop and i++ is a single iinc.
  • while (n != 1) leaves with if_icmpeq, the opposite of the source condition.
  • doWhile jumps back with ifne while the condition holds: the only loop here whose branch says what the source says.
  • Backward branches are what the JVM counts to decide that a loop is hot enough to compile to machine code.

Loops.class

static int forLoop(int n)

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

4int sum = 0;
0iconst_0
1istore_1 // sum
5for (int i = 0; i < n; i++) {
2iconst_0
3istore_2 // i
4iload_2 // i
5iload_0 // n
6if_icmpge 19
6sum += i;
9iload_1 // sum
10iload_2 // i
11iadd
12istore_1 // sum
5for (int i = 0; i < n; i++) {
13iinc 2, 1// i
16goto 4
8return sum;
19iload_1 // sum
20ireturn

Open in the viewer

static int whileLoop(int n)

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

12int steps = 0;
0iconst_0
1istore_1 // steps
13while (n != 1) {
2iload_0 // n
3iconst_1
4if_icmpeq 31
14n = n % 2 == 0 ? n / 2 : 3 * n + 1;
7iload_0 // n
8iconst_2
9irem
10ifne 19
13iload_0 // n
14iconst_2
15idiv
16goto 24
19iconst_3
20iload_0 // n
21imul
22iconst_1
23iadd
24istore_0 // n
15steps++;
25iinc 1, 1// steps
28goto 2
17return steps;
31iload_1 // steps
32ireturn

Open in the viewer

static int doWhile(int n)

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

21int digits = 0;
0iconst_0
1istore_1 // digits
23n /= 10;
2iload_0 // n
3bipush 10
5idiv
6istore_0 // n
24digits++;
7iinc 1, 1// digits
25} while (n != 0);
10iload_0 // n
11ifne 2
26return digits;
14iload_1 // digits
15ireturn

Open in the viewer

← Boolean logic and the ternary operatorAll topicsinstanceof patterns →

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.