HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › Switch on a String

What javac does

Switch on a String

A switch on hashCode(), equals() to confirm, then a second switch.

A switch on a String (Java 7) compiles into two switches. The first is a lookupswitch on s.hashCode(); each of its cases calls equals to rule out a hash collision and sets a hidden index. The second is a tableswitch on that index, which jumps to your code.

The source

// A switch on a String: a switch on hashCode() first, then equals(), then a second switch.
public class StringSwitch {
    static int days(String month) {
        switch (month) {
            case "feb":
                return 28;
            case "apr":
            case "jun":
            case "sep":
            case "nov":
                return 30;
            default:
                return 31;
        }
    }
}

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

What to look for

  • The hash codes are computed by javac: 101251 is "feb".hashCode().
  • javac copies the string into a hidden local and starts the index at -1 (iconst_m1); a string that matches nothing keeps -1 and lands on the default.
  • Cases that share a body share a target: indexes 1 to 4 all jump to return 30.
  • Because the first thing it does is call hashCode(), switching on a null String throws NullPointerException.

StringSwitch.class

static int days(String month)

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

4switch (month) {
0aload_0 // month
1astore_1
2iconst_m1
3istore_2
4aload_1
5invokevirtual #7// String.hashCode() : int
8lookupswitch { 96803→74, 101251→60, 105603→88, 109269→116, 113758→102, default→127 }
60aload_1
61ldc #13// "feb"
63invokevirtual #15// String.equals(Object) : boolean
66ifeq 127
69iconst_0
70istore_2
71goto 127
74aload_1
75ldc #19// "apr"
77invokevirtual #15// String.equals(Object) : boolean
80ifeq 127
83iconst_1
84istore_2
85goto 127
88aload_1
89ldc #21// "jun"
91invokevirtual #15// String.equals(Object) : boolean
94ifeq 127
97iconst_2
98istore_2
99goto 127
102aload_1
103ldc #23// "sep"
105invokevirtual #15// String.equals(Object) : boolean
108ifeq 127
111iconst_3
112istore_2
113goto 127
116aload_1
117ldc #25// "nov"
119invokevirtual #15// String.equals(Object) : boolean
122ifeq 127
125iconst_4
126istore_2
127iload_2
128tableswitch { 0→164, 1→167, 2→167, 3→167, 4→167, default→170 }
6return 28;
164bipush 28
166ireturn
11return 30;
167bipush 30
169ireturn
13return 31;
170bipush 31
172ireturn

Open in the viewer

← Lambdas and method referencesAll topicsSwitch on an enum →

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.