Java OR 运算符在 switch-case 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18412936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:23:54  来源:igfitidea点击:

OR operator in switch-case?

javaswitch-statement

提问by Droidman

Let's take a simple switch-case that looks like:

让我们看一个简单的 switch-case,它看起来像:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.someValue :
        case R.id.someOtherValue:
            // do stuff
            break;
    }
}

I wonder why it is not allowed to use the ||operator? Like

我想知道为什么不允许使用||运算符?喜欢

switch (v.getId()) {
    case R.id.someValue || R.id.someOtherValue:
        // do stuff
        break;
}

The switch-caseconstruct is pretty similar to an if-elsestatement, you can use the OR operator in an ifhowever. What are the backgrounds for a switch-caseto not accept this operator?

switch-case构造与if-else语句非常相似,您可以在if然而中使用 OR 运算符。switch-case不接受这个运营商的背景是​​什么?

采纳答案by Rohit Jain

What are the backgrounds for a switch-case to not accept this operator?

switch-case 不接受这个操作符的背景是什么?

Because caserequires constant expression as its value. And since an ||expression is not a compile time constant, it is not allowed.

因为case需要常量表达式作为其值。并且由于||表达式不是编译时常量,因此是不允许的。

From JLS Section 14.11:

来自JLS 第 14.11 节

Switch label should have following syntax:

开关标签应具有以下语法:

SwitchLabel:
case ConstantExpression :
case EnumConstantName :
default :

SwitchLabel:
case ConstantExpression :
case EnumConstantName :
default :



Under the hood:

引擎盖下:

The reason behind allowing just constant expression with cases can be understood from the JVM Spec Section 3.10 - Compiling Switches:

JVM 规范第 3.10 节 - 编译开关可以理解仅允许使用 case 的常量表达式背后的原因:

Compilation of switch statements uses the tableswitchand lookupswitchinstructions. The tableswitch instruction is used when the cases of the switch can be efficiently represented as indices into a table of target offsets. The default target of the switch is used if the value of the expression of the switch falls outside the range of valid indices.

switch 语句的编译使用tableswitchlookupswitch指令。当切换的情况可以有效地表示为目标偏移量表的索引时,将使用 tableswitch 指令。如果 switch 表达式的值落在有效索引范围之外,则使用 switch 的默认目标。

So, for the cases label to be used by tableswitchas a index into the table of target offsets, the value of the case should be known at compile time. That is only possible if the case value is a constant expression. And ||expression will be evaluated at runtime, and the value will only be available at that time.

因此,对于要用作tableswitch目标偏移表索引的案例标签,案例的值应该在编译时知道。只有当 case 值是一个常量表达式时才有可能。并且||表达式将在运行时计算,并且该值仅在那个时候可用。

From the same JVM section, the following switch-case:

从同一个 JVM 部分,以下内容switch-case

switch (i) {
    case 0:  return  0;
    case 1:  return  1;
    case 2:  return  2;
    default: return -1;
}

is compiled to:

编译为:

0   iload_1             // Push local variable 1 (argument i)
1   tableswitch 0 to 2: // Valid indices are 0 through 2  (NOTICE This instruction?)
      0: 28             // If i is 0, continue at 28
      1: 30             // If i is 1, continue at 30
      2: 32             // If i is 2, continue at 32
      default:34        // Otherwise, continue at 34
28  iconst_0            // i was 0; push int constant 0...
29  ireturn             // ...and return it
30  iconst_1            // i was 1; push int constant 1...
31  ireturn             // ...and return it
32  iconst_2            // i was 2; push int constant 2...
33  ireturn             // ...and return it
34  iconst_m1           // otherwise push int constant -1...
35  ireturn             // ...and return it

So, if the casevalue is not a constant expressions, compiler won't be able to index it into the table of instruction pointers, using tableswitchinstruction.

因此,如果该case值不是常量表达式,编译器将无法使用tableswitch指令将其索引到指令指针表中。

回答by spt025

dude do like this

伙计这样做

    case R.id.someValue :
    case R.id.someOtherValue :
       //do stuff

This is same as using OR operator between two values Because of this case operator isn't there in switch case

这与在两个值之间使用 OR 运算符相同,因为在 switch case 中没有这种 case 运算符

回答by Prashant Bhate

Switch is not same as if-else-if.

switch 与 if-else-if 不同。

Switch is used when there is one expression that gets evaluated to a value and that value can be one of predefined set of values. If you need to perform multiple boolean / comparions operations run-time then if-else-if needs to be used.

当有一个表达式被评估为一个值并且该值可以是预定义的一组值之一时,使用 Switch。如果您需要在运行时执行多个布尔/比较操作,则需要使用 if-else-if。

回答by Breimer

foreach (array('one', 'two', 'three') as $v) {
    switch ($v) {
        case (function ($v) {
            if ($v == 'two') return $v;
            return 'one';
        })($v):
            echo "$v min \n";
            break;


    }
}

this works fine for languages supporting enclosures

这适用于支持外壳的语言

回答by Hilaj

You cannot use || operators in between 2 case. But you can use multiple case values without using a break between them. The program will then jump to the respective case and then it will look for code to execute until it finds a "break". As a result these cases will share the same code.

您不能使用 || 运算符介于 2 种情况之间。但是您可以使用多个 case 值而无需在它们之间使用中断。然后程序将跳转到相应的案例,然后它会寻找要执行的代码,直到找到“中断”。因此,这些案例将共享相同的代码。

switch(value) 
{ 
    case 0: 
    case 1: 
        // do stuff for if case 0 || case 1 
        break; 
    // other cases 
    default: 
        break; 
}