简体   繁体   English

为什么不能在没有赋值的情况下使用三元运算符(左边是变量)?

[英]Why can't Ternary operator be used without assignment (variable on the left)?

I have searched for this and there are some questions regarding the same problem, but none of the answers to those questions seem to address my query. 我已经搜索了这个问题,并且对于同样的问题存在一些问题,但这些问题的答案似乎都没有解决我的问题。

I have looked up the spec and managed to find the following points: 我已经查看了规范,并设法找到以下几点:

  • first expression to ternary must be of type boolean 第一个表达式到三元必须是boolean类型

  • second and third expressions cant be calls to void methods. 第二个和第三个表达式无法调用void方法。

given the above information if I write the following code 如果我写下面的代码,请给出上述信息

String res;
System.out.println(res="walter");

it prints walter to console, meaning the expression returned something hence its not void. 它将walter打印到控制台,这意味着表达式返回了一些东西,因此它不是空的。 but now if try to write this 但现在如果试着写这个

String stuff = "TV";
String res=null;
stuff.equals ("TV") ? res= "Walter" :  res = "White" ;

this code fails to compile with The left-hand side of an assignment must be a variable 此代码无法编译。赋值的左侧必须是变量

Even though both of above conditions are met(to best of my knowledge). 即使满足上述两个条件(据我所知)。 why the code doesn't compile and why it requires a variable on the left ? 代码为什么不编译以及为什么它需要左边的变量?

moreover if I do this 而且如果我这样做的话

res = stuff.equals("TV")?res="WALTER":res="WHITE";

the code fails to compile with 代码无法编译

The operator <= is undefined for the argument type(s) java.lang.String,java.lang.String 对于参数类型java.lang.String,java.lang.String,运算符<=未定义

but following compiles fine 但是以下编译好了

res = stuff.equals("TV")?res="WALTER":"WHITE";

PS PS

  • Why there must be variable on the left side of ternary operator, method return values can be discarded why cant this be done in the case of ternary. 为什么三元运算符的左侧必须有变量,方法返回值可以被丢弃,为什么不能在三元运算的情况下完成。
  • Why Java doesn't allow it, what problems or inconsistencies will it cause if allowed 为什么Java不允许它,如果允许它会导致什么问题或不一致

The conditional operator is an expression: it has a result: 条件运算符是一个表达式:它有一个结果:

int a = cond ? 1 : 2;

but you can't use it like this: 但你不能这样使用它:

cond ? 1 : 2;

because it's not a StatementExpression ; 因为它不是StatementExpression ; this is much the same as the fact you can't write any of: 这与你不能写任何一个事实相同:

1;
2 * 3;
array[1];

because they just doesn't serve any purpose. 因为他们只是没有任何目的。

A StatementExpression is an expression that you can pop a ; StatementExpression是一个可以弹出的表达式; after, for example: 之后,例如:

int i = 0;
someMethod(i++);  // Use of i++ as an expression.
i++;              // Use of i++ as a StatementExpression.

The full list of StatementExpression s can be found in the language spec : 可以在语言规范中找到StatementExpression的完整列表:

StatementExpression:
    Assignment 
    PreIncrementExpression 
    PreDecrementExpression 
    PostIncrementExpression 
    PostDecrementExpression 
    MethodInvocation 
    ClassInstanceCreationExpression

As such, you don't have to have an assignment: you can use the conditional operator in a contrived way such as this: 因此,您不必具有赋值:您可以以人为的方式使用条件运算符,例如:

(true ? new int[1] : null)[0]++;

(Not that I am in any way advocating this as good code, or in any way useful; merely pointing out that it is legal) (并不是说我以任何方式提倡这是好的代码,或者以任何方式有用;只是指出它是合法的)


As for the rest of your issues: these are just compiler-implementation-specific messages. 至于其他问题:这些只是特定于编译器实现的消息。 Your compiler is tripping over the invalid syntax, and doing its best to help you, but it is not doing an especially good job. 你的编译器绊倒了无效的语法,并尽力帮助你,但它并没有做得特别好。

Note that other compilers (eg the one used by Ideone) give totally different messages. 请注意,其他编译器(例如Ideone使用的编译器)会给出完全不同的消息。

The first form should be written using an if/else: 第一个表单应使用if / else编写:

if (stuff.equals ("TV")) res= "Walter" else res = "White" ;

( if is a statement, incidentally) if是声明,顺便说一句)

The second one is just missing some parentheses: 第二个是缺少一些括号:

res = stuff.equals("TV")?(res="WALTER"):(res="WHITE");

Although the assignments in the second and third operands are redundant anyway: 虽然第二和第三个操作数中的赋值仍然是多余的:

res = stuff.equals("TV")?"WALTER":"WHITE";    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM