HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › Generics and bridge methods

What javac does

Generics and bridge methods

Erased types, the casts javac inserts, and a synthetic bridge method.

Generic types are erased: in bytecode, List<String> is just List, and get returns Object. javac inserts a checkcast wherever your code relies on the type argument. And when a class implements a generic interface for a specific type, javac generates a bridge method so that calls through the erased interface still reach your code.

The source

// Generics: erased types, casts javac inserts, and a bridge method.
import java.util.ArrayList;
import java.util.List;

public class Bridge implements Comparable<Bridge> {
    final int rank;

    Bridge(int rank) {
        this.rank = rank;
    }

    @Override
    public int compareTo(Bridge other) {
        return Integer.compare(rank, other.rank);
    }

    static String first(List<String> names) {
        return names.get(0);
    }

    static List<String> make() {
        return new ArrayList<>();
    }
}

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

What to look for

  • first() calls List.get, then checkcast String.
  • compareTo(Object) is a synthetic bridge method (ACC_BRIDGE). Callers through Comparable invoke the erased compareTo(Object); the bridge casts its argument to Bridge and calls your compareTo(Bridge).
  • new ArrayList<>() is a plain new ArrayList: type arguments do not exist in the bytecode.
  • The generic types survive only in Signature attributes, for reflection and for compilers reading the class.

Bridge.class

Bridge(int rank)

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

8Bridge(int rank) {
0aload_0 // this
1invokespecial #1// new Object()
9this.rank = rank;
4aload_0 // this
5iload_1 // rank
6putfield #7// Bridge.rank : int
10}
9return

Open in the viewer

public int compareTo(Bridge other)

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

14return Integer.compare(rank, other.rank);
0aload_0 // this
1getfield #7// Bridge.rank : int
4aload_1 // other
5getfield #7// Bridge.rank : int
8invokestatic #13// Integer.compare(int, int) : int
11ireturn

Open in the viewer

static String first(List names)

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

18return names.get(0);
0aload_0 // names
1iconst_0
2invokeinterface #19, 2// List.get(int) : Object
7checkcast #25// String
10areturn

Open in the viewer

static List make()

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

22return new ArrayList<>();
0new #27// ArrayList
3dup
4invokespecial #29// new ArrayList()
7areturn

Open in the viewer

public int compareTo(Object) bridge synthetic

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

5public class Bridge implements Comparable<Bridge> {
0aload_0 // this
1aload_1
2checkcast #8// Bridge
5invokevirtual #30// Bridge.compareTo(Bridge) : int
8ireturn

Open in the viewer

← assertAll topicsBoolean logic and the ternary operator →

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.