Created
December 23, 2012 11:27
-
-
Save galak-fyyar/4363003 to your computer and use it in GitHub Desktop.
How switch by String is implemented in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StringTest { | |
public static void main(String[] args) { | |
String switchString = "StringTest"; | |
switch (switchString) { | |
case "FirstString": return; | |
case "SecondString": return; | |
case "StringTest": | |
System.out.println("Here it is"); | |
return; | |
default: | |
return; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Compiled from "StringTest.java" | |
class StringTest { | |
StringTest(); | |
Code: | |
0: aload_0 | |
1: invokespecial #1 // Method java/lang/Object."<init>":()V | |
4: return | |
public static void main(java.lang.String[]); | |
Code: | |
0: ldc #2 // String StringTest | |
2: astore_1 | |
3: aload_1 | |
4: astore_2 | |
5: iconst_m1 | |
6: istore_3 | |
7: aload_2 | |
8: invokevirtual #3 // Method java/lang/String.hashCode:()I | |
11: lookupswitch { // 3 | |
-539968059: 58 | |
420381795: 72 | |
634782401: 44 | |
default: 83 | |
} | |
44: aload_2 | |
45: ldc #4 // String FirstString | |
47: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z | |
50: ifeq 83 | |
53: iconst_0 | |
54: istore_3 | |
55: goto 83 | |
58: aload_2 | |
59: ldc #6 // String SecondString | |
61: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z | |
64: ifeq 83 | |
67: iconst_1 | |
68: istore_3 | |
69: goto 83 | |
72: aload_2 | |
73: ldc #2 // String StringTest | |
75: invokevirtual #5 // Method java/lang/String.equals:(Ljava/lang/Object;)Z | |
78: ifeq 83 | |
81: iconst_2 | |
82: istore_3 | |
83: iload_3 | |
84: tableswitch { // 0 to 2 | |
0: 112 | |
1: 113 | |
2: 114 | |
default: 123 | |
} | |
112: return | |
113: return | |
114: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream; | |
117: ldc #8 // String Here it is | |
119: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V | |
122: return | |
123: return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment