一个开关Java问题:案件的前pressions必须是恒定的前pressions案件、问题、Java、pressions

2023-09-04 03:01:56 作者:有些人′只是个喷嚏

我有我的开关/ case语句的一个问题。错误说:案前pressions必须是恒定的前pressions。我理解的错误,我可以使用。如果解决这个问题,但能有人告诉我,为什么这样EX pression必须在一个开关/箱不变。 一个code我的错误的例子:

I having a problem in my switch/case statement. 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. 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你的解释!

Thx for your explanation !!

推荐答案

因此​​,它可以在编译期进行评估(静态检查)

So it can be evaluated during the compilation phase ( statically check )

请参阅:http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.11为开关

此外,它可以帮助你更好地了解怎么说开关转换成字节code:

Additionally it may help you to understand better how that switch is transformed into bytecode:

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;
    }
  }
}

和编译后:

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

}

请参阅标记为该行1:

 1:   lookupswitch{ //2
            1: 28;
            9: 39;
            default: 50 }

有评估值,然后进到一些其它线路。例如,如果值 9 会跳转到指令39:

It evaluates the value and goes to some other line. For instance if value is 9 it will jump to instruction 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

这反过来又跳转到指令58:

Which in turn jumps to instruction 58 :

   58:  return

所有这将是不可能的,如果它被动态地进行评价。这就是为什么。

All this wouldn't be possible if it was evaluated dynamically. That's why.