这个功能是否会被放入以后的Java版本中?
有人可以解释为什么我不能这样做,就像Java的switch声明的技术方式一样吗?
我在编译开关时阅读了JVM规范,并对如何编译String上的switch语句感兴趣.这是我检查的测试方法(JDK1.7.0_40):
static int test(String i) {
switch (i) {
case "a": return -100;
case "45b": return 1;
case "c": return 2;
default: return -1;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这个方法能够在string的hashCode上编译成简单的lookupswitch,但突然之间
static int test(java.lang.String);
Code:
0: aload_0
1: astore_1
2: iconst_m1
3: istore_2
4: aload_1
5: invokevirtual #6 // Method java/lang/String.hashCode:()I
8: lookupswitch { // 3
97: 44
99: 72
51713: 58
default: 83
}
44: aload_1
45: ldc #7 // String a
47: invokevirtual #8 // Method java/lang/String.equals:(Ljava/lang/Object;)Z
50: ifeq 83 …Run Code Online (Sandbox Code Playgroud)