[英]compilation error with ternary operator
I tried the following with ternary operator and I do not understand why it is not compiling. 我尝试使用三元运算符以下,但我不明白为什么它不编译。 The issue seems so small but I do not understand and hence bothers me -
这个问题似乎很小,但我不明白,因此困扰我-
Line 1 --> int a = false ? 第1行 -> int a = false? y+=1 : (x*=10);
y + = 1:(x * = 10);
Line 2 --> int b = false ? 第2行 -> int b = false? y+=1 : x*=10;
y + = 1:x * = 10;
Line 1 compiles however Line 2 does not. 第1行编译,但是第2行不编译。 Why ?
为什么呢
How is the parenthesis making a difference in case of 3rd operand and not the second operand. 在第3个操作数而不是第2个操作数的情况下,括号有何不同? I didn't have to use parenthesis with anything else in the 2nd / 3rd operands (Unary, string, basic arithmetic ...) Why just assignment operator and that too specifically 3rd operand ?
我不必在第二/第三操作数中使用括号与其他任何内容(一元,字符串,基本算术...)为什么只使用赋值运算符,而又太具体地使用第三操作数呢?
Thanks in advance ! 提前致谢 !
Without the ()
around x*=10
, the entire left-hand operand of the *=
operator is false ? y+=1 : x
如果在
x*=10
周围没有()
,则*=
运算符的整个左侧操作数为false ? y+=1 : x
false ? y+=1 : x
, as though you had: false ? y+=1 : x
,就好像您有:
int b = (false ? y+=1 : x)*=10;
And as false ? y+=1 : x
和
false ? y+=1 : x
false ? y+=1 : x
isn't a variable, it can't be the left-hand operand of *=
. false ? y+=1 : x
不是变量,不能是*=
的左操作数。
The assignment operators (including compound assignment, *=
and such) are very, very low in the precedence list , below the conditional operator ( ? :
): 在优先级列表中 ,条件运算符(
? :
:)下的赋值运算符(包括复合赋值, *=
等)非常低:
Operators Precedence
运算符优先
- postfix:
expr ++ expr --
后缀:
expr ++ expr --
- unary:
++ expr -- expr + expr - expr ~ !
一元:
++ expr -- expr + expr - expr ~ !
- multiplicative:
* / %
乘法:
* / %
- additive:
+ -
加成物:
+ -
- shift:
<< >> >>>
班次:
<< >> >>>
- relational:
< > <= >= instanceof
关系:
< > <= >= instanceof
- equality:
== !=
平等:
== !=
- bitwise: AND
&
按位:AND
&
- bitwise: exclusive OR
^
按位:异或
^
- bitwise: inclusive OR
|
按位:包含或
|
- logical: AND
&&
逻辑:AND
&&
- logical: OR
||
逻辑:OR
||
- ternary:
? :
三元:
? :
? :
- assignment:
= += -= *= /= %= &= ^= |= <<= >>= >>>=
分配:
= += -= *= /= %= &= ^= |= <<= >>= >>>=
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.