三元运算符,使用赋值时语法错误 [英] Ternary operator, syntax error when using assignment

查看:108
本文介绍了三元运算符,使用赋值时语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的下面3行代码编译OK。 (请注意,该代码是人工Java编码的一个示例,因此在专业编写的代码中不会出现。)

The following 3 lines of code below compile OK. (Please note that this code is an example of "artificial Java coding", and consequently wouldn't be seen in professionally written code.)

int x, y;
boolean b=true;

x = b ? y=1 : 2;  // Compiles OK.

如果我现在更改上面第3行中的代码,则看起来像下面的代码行

If I now change the code in line #3 above, so that it looks like the following code line below, the compiler generates an error.

// Change the position of the "y assignment", and now the code doesn't compile.
x = b ? 1 : y=2;  

以下是语法错误消息:

有人可以解释吗?这种行为(对于菜鸟Java学习者)?谢谢。

Can someone please explain this behaviour (to a rookie Java learner)? Thank you.

推荐答案

这是由于运算符的优先级。第一种情况等于:

This is because of operator precedence. The first case is equal to this:

x = (b ? (y=1) : 2);  // Compiles OK.

而第二个是:

x = (b ? 1 : y) = 2;  

第一个编译的确不错,因为赋值被赋值为新值。因此,如果 b 为true,则将同时导致 x y 等于1。第二个像说: x = 1 = 2 。因此,是的,要解决此编译器错误,请在您的语句中添加括号:

The first one compiles indeed fine, because an assignment gets evaluated to the new value. So, if b is true, it will cause to be both x and y equal to 1. The second one is like saying: x = 1 = 2. So, yes, to fix this compiler error, add paratheses to your statement:

x = (b ? 1 : (y = 2));  // Outer () are not needed, but is clearer, IMHO.




更长

首先, Java中的运算符优先级说,赋值运算符的优先级低于条件三进制操作员。这意味着您的第二个表达式等效于:

First of all, operator precedence in Java says that the assignment operators have lower priority than the conditional ternary operator. This means that your second expression is equivalent to:

x = (b ? 1 : y) = 2;

您可以看到,这显然是错误的。实际上, JLS§15.26这样说:

As you can see, this looks plain wrong. Indeed, JLS §15.26 says this:


有12个赋值运算符;在语法上都是 right-associative (它们从右到左分组)。因此, a = b = c 表示 a =(b = c),该值分配了 c b ,然后将 b 的值分配给 a

There are 12 assignment operators; all are syntactically right-associative (they group right-to-left). Thus, a=b=c means a=(b=c), which assigns the value of c to b and then assigns the value of b to a.

赋值运算符的第一个操作数的结果必须为变量或编译时发生错误。 (这说明了您遇到的编译时错误)

The result of the first operand of an assignment operator must be a variable, or a compile-time error occurs. (This explains the compile time error you face)

在运行时,赋值表达式的结果为分配发生后的变量。赋值表达式的结果本身并不是变量。

At run time, the result of the assignment expression is the value of the variable after the assignment has occurred. The result of an assignment expression is not itself a variable.

应用右关联:

x = ((b ? 1 : y) = 2);

最后我们可以看到为什么会生成编译器错误:三元条件运算符的结果是不是变量(据我所知,它实际上不在JLS中,但是编译器会在一个简单的测试用例中告诉您: https://ideone.com/kMr7Xv )。

Finally we can see why this generates a compiler error: the result of a ternary conditional operator is not a variable (which is actually not in the JLS as far as I can find, however the compiler tells you in a simple test case like this one: https://ideone.com/kMr7Xv).

这篇关于三元运算符,使用赋值时语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆