HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › Autoboxing

What javac does

Autoboxing

Integer.valueOf on the way in, intValue() on the way out, and a checkcast in between.

Autoboxing is method calls that javac writes for you: Integer.valueOf(int) to box, intValue() to unbox. Collections hold Objects, so after next() or get() comes a checkcast to Integer before the unboxing call.

The source

// Autoboxing: Integer.valueOf on the way in, intValue() on the way out.
import java.util.ArrayList;
import java.util.List;

public class Boxing {
    static int sum(List<Integer> values) {
        int total = 0;
        for (Integer v : values) {
            total += v;
        }
        return total;
    }

    static List<Integer> squares(int n) {
        List<Integer> out = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            out.add(i * i);
        }
        return out;
    }

    static boolean same(Integer a, Integer b) {
        return a == b;
    }
}

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 squares, out.add(i * i) is imul, invokestatic Integer.valueOf, invokeinterface List.add, then pop: add returns a boolean nobody uses.
  • same(a, b) compiles to if_acmpne: it compares references, not values. valueOf returns cached objects for -128 to 127, so it is true for small numbers and false for large ones.
  • Unboxing null throws NullPointerException from intValue(); there is no null check in the bytecode.

Boxing.class

static int sum(List values)

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

7int total = 0;
0iconst_0
1istore_1 // total
8for (Integer v : values) {
2aload_0 // values
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// Integer
27astore_3 // v
9total += v;
28iload_1 // total
29aload_3 // v
30invokevirtual #25// Integer.intValue() : int
33iadd
34istore_1 // total
10}
35goto 9
11return total;
38iload_1 // total
39ireturn

Open in the viewer

static List squares(int n)

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

15List<Integer> out = new ArrayList<>();
0new #29// ArrayList
3dup
4invokespecial #31// new ArrayList()
7astore_1 // out
16for (int i = 0; i < n; i++) {
8iconst_0
9istore_2 // i
10iload_2 // i
11iload_0 // n
12if_icmpge 34
17out.add(i * i);
15aload_1 // out
16iload_2 // i
17iload_2 // i
18imul
19invokestatic #32// Integer.valueOf(int) : Integer
22invokeinterface #36, 2// List.add(Object) : boolean
27pop
16for (int i = 0; i < n; i++) {
28iinc 2, 1// i
31goto 10
19return out;
34aload_1 // out
35areturn

Open in the viewer

static boolean same(Integer a, Integer b)

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

23return a == b;
0aload_0 // a
1aload_1 // b
2if_acmpne 9
5iconst_1
6goto 10
9iconst_0
10ireturn

Open in the viewer

← Switch on an enumAll topicsThe enhanced for loop →

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.