HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › x++ and friends

What javac does

x++ and friends

iinc for locals; read, add and write for fields and arrays, with dup to keep the value.

x++ compiles very differently depending on what x is. An int local variable has its own instruction, iinc, which changes the variable in place without touching the stack. Fields and array elements need a read, an add and a write, plus dup instructions to keep the expression's value.

The source

// x++ in its many forms: iinc for locals, read-modify-write for fields and arrays.
public class Increments {
    int field;
    static long counter;

    static int local(int i) {
        i++;
        int a = i++;
        int b = ++i;
        return a + b;
    }

    int field() {
        return field++;
    }

    static long staticLong() {
        return ++counter;
    }

    static void array(int[] a, int i) {
        a[i]++;
    }

    static void big(int i) {
        i += 1000;
        System.out.println(i);
    }
}

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 local(), i++, i++ and ++i are all iinc 0, 1. The only difference is whether the iload that reads the value comes before or after.
  • return field++ uses dup_x1 to keep the old value underneath the object reference that putfield consumes.
  • return ++counter on a static long uses dup2 to copy the new two-slot value.
  • a[i]++ uses dup2 to keep the array and the index for the store.
  • i += 1000 does not fit iinc's one-byte constant, so it becomes wide iinc.

Increments.class

static int local(int i)

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

7i++;
0iinc 0, 1// i
8int a = i++;
3iload_0 // i
4iinc 0, 1// i
7istore_1 // a
9int b = ++i;
8iinc 0, 1// i
11iload_0 // i
12istore_2 // b
10return a + b;
13iload_1 // a
14iload_2 // b
15iadd
16ireturn

Open in the viewer

int field()

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

14return field++;
0aload_0 // this
1dup
2getfield #7// Increments.field : int
5dup_x1
6iconst_1
7iadd
8putfield #7// Increments.field : int
11ireturn

Open in the viewer

static long staticLong()

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

18return ++counter;
0getstatic #13// Increments.counter : long
3lconst_1
4ladd
5dup2
6putstatic #13// Increments.counter : long
9lreturn

Open in the viewer

static void array(int[] a, int i)

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

22a[i]++;
0aload_0 // a
1iload_1 // i
2dup2
3iaload
4iconst_1
5iadd
6iastore
23}
7return

Open in the viewer

static void big(int i)

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

26i += 1000;
0wide iinc 0, 1000// i
27System.out.println(i);
6getstatic #17// System.out : PrintStream
9iload_0 // i
10invokevirtual #23// PrintStream.println(int) : void
28}
13return

Open in the viewer

← Comparing longs, floats and doublesAll topics

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.