HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › Pattern-matching switch

What javac does

Pattern-matching switch

SwitchBootstraps.typeSwitch picks the case; guards jump back and try again.

A switch on types cannot use a table of constants, so javac (Java 21) asks the JDK which case matches: an invokedynamic to SwitchBootstraps.typeSwitch, given the object and a case index to start from, returns the index of the first case whose type matches, or -1 for null. A tableswitch then jumps to that case, which casts the object and binds the variable.

The source

// min-release: 21
// A pattern switch: one invokedynamic picks the case, then each case casts and binds.
public class PatternSwitch {
    static String describe(Object o) {
        return switch (o) {
            case null -> "null";
            case Integer i when i > 100 -> "a big int";
            case Integer i -> "the int " + i;
            case String s -> "a string of length " + s.length();
            default -> "something else";
        };
    }
}

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

What to look for

  • case null is index -1 in the tableswitch.
  • The guard (when i > 100) is ordinary code inside the case. If it fails, javac sets the start index to 1 and jumps back to call typeSwitch again, so matching resumes at the next case.
  • The bootstrap arguments are the case types in order: Integer, Integer, String.

PatternSwitch.class

static String describe(Object o)

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

5return switch (o) {
0aload_0 // o
1astore_1
2iconst_0
3istore_2
4aload_1
5iload_2
6invokedynamic #7// typeSwitch(Object, int) : int ⇐ SwitchBootstraps.typeSwitch
11tableswitch { -1→40, 0→45, 1→69, 2→85, default→104 }
6case null -> "null";
40ldc #11// "null"
42goto 109
7case Integer i when i > 100 -> "a big int";
45aload_1
46checkcast #13// Integer
49astore_3 // i
50aload_3 // i
51invokevirtual #15// Integer.intValue() : int
54bipush 100
56if_icmpgt 64
59iconst_1
60istore_2
61goto 4
64ldc #19// "a big int"
66goto 109
8case Integer i -> "the int " + i;
69aload_1
70checkcast #13// Integer
73astore 4// i
75aload 4// i
77invokedynamic #21// makeConcatWithConstants(Integer) : String ⇐ StringConcatFactory.makeConcatWithConstants "the int \1"
82goto 109
9case String s -> "a string of length " + s.length();
85aload_1
86checkcast #25// String
89astore 5// s
91aload 5// s
93invokevirtual #27// String.length() : int
96invokedynamic #30// makeConcatWithConstants(int) : String ⇐ StringConcatFactory.makeConcatWithConstants "a string of length \1"
101goto 109
10default -> "something else";
104ldc #33// "something else"
106goto 109
5return switch (o) {
109areturn

Open in the viewer

The bootstrap methods

From the BootstrapMethods attribute of the --release 25 class: each invokedynamic names one of these by index.

  1. SwitchBootstraps.typeSwitch(MethodHandles$Lookup, String, MethodType, Object[]) : CallSite
    • Class Integer
    • Class Integer
    • Class String
  2. StringConcatFactory.makeConcatWithConstants(MethodHandles$Lookup, String, MethodType, String, Object[]) : CallSite
    • String "the int \u0001"
  3. StringConcatFactory.makeConcatWithConstants(MethodHandles$Lookup, String, MethodType, String, Object[]) : CallSite
    • String "a string of length \u0001"
← RecordsAll topicsCompile-time constants →

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.