[英]A Switch Java problem : case expressions must be constant expressions
I having a problem in my switch/case statement. 我在switch / case语句中遇到问题。 The error says : "Case expressions must be constant expressions".
错误说:“案例表达式必须是常量表达式”。 I understand the error and I can resolve it using If but can someone tells me why the case expression must be constant in a switch/case.
我理解错误,我可以使用If解决它,但有人可以告诉我为什么case表达式必须在switch / case中保持不变。 A code example of my error :
我的错误的代码示例:
public boolean onOptionsItemSelected(MenuItem item) {
int idDirectory = ((MenuItem) findViewById(R.id.createDirectory)).getItemId();
int idSuppression = ((MenuItem) findViewById(R.id.recycleTrash)).getItemId();
int idSeeTrash = ((MenuItem) findViewById(R.id.seeTrash)).getItemId();
switch (item.getItemId()) {
case idDirectory:
createDirectory(currentDirectory);
break;
case idSuppression:
recycleTrash();
break;
case idSeeTrash:
seeTrash();
break;
}
return super.onOptionsItemSelected(item);
}
Thx for your explanation !! 谢谢你的解释!!
So it can be evaluated during the compilation phase ( statically check ) 所以它可以在编译阶段进行评估(静态检查)
See: http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.11 for a formal definition of the switch
. 有关
switch
的正式定义,请参阅: http : //docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.11 。
Additionally it may help you to understand better how that switch
is transformed into bytecode: 此外,它可以帮助您更好地了解该
switch
如何转换为字节码:
class Switch {
void x(int n ) {
switch( n ) {
case 1: System.out.println("one"); break;
case 9: System.out.println("nine"); break;
default: System.out.println("nothing"); break;
}
}
}
And after compiling: 编译完成后:
C:\>javap -c Switch
Compiled from "Switch.java"
class Switch extends java.lang.Object{
Switch();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
void x(int);
Code:
0: iload_1
1: lookupswitch{ //2
1: 28;
9: 39;
default: 50 }
28: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
31: ldc #3; //String one
33: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
36: goto 58
39: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
42: ldc #5; //String nine
44: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
47: goto 58
50: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
53: ldc #6; //String nothing
55: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
58: return
}
See that line marked as 1:
看到标记为
1:
那一行1:
1: lookupswitch{ //2
1: 28;
9: 39;
default: 50 }
It evaluates the value and goes to some other line. 它评估价值并转到其他一些行。 For instance if value is
9
it will jump to instruction 39: 例如,如果值为
9
,它将跳转到指令39:
39: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
42: ldc #5; //String nine
44: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
47: goto 58
Which in turn jumps to instruction 58 : 反过来跳到指令58:
58: return
All this wouldn't be possible if it was evaluated dynamically. 如果动态评估,所有这些都是不可能的。 That's why.
这就是为什么。
在eclipse IDE中很简单,在切换句子CTRL + 1并转换切换句子 - if-else句子http://tools.android.com/tips/non-constant-fields
The idDirectory
and others need to be a constant and not a declared variable. idDirectory
和其他需要是常量而不是声明的变量。 Switch
will not work in this case, you need switch to if-else
construct. 在这种情况下,
Switch
不起作用,需要切换到if-else
构造。
EDIT I see what OP meant. 编辑我看到OP意味着什么。 That is just how switch works in java language.
这就是交换机在java语言中的工作原理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.