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

mightyWOZ :

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

  • second and third expressions cant be calls to void methods.

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. 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

but following compiles fine

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

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
Andy Turner :

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; 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 ; 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 StatementExpressions can be found in the language spec:

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 (e.g. the one used by Ideone) give totally different messages.

The first form should be written using an if/else:

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

(if is a statement, incidentally)

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=113816&siteId=1