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.
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;
Here is the syntax error message:
Can someone please explain this behaviour (to a rookie Java learner)? Thank you.
Short:
This is because of operator precedence. The first case is equal to this:
While the second is:
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 bothx
andy
equal to 1. The second one is like saying:x = 1 = 2
. So, yes, to fix this compiler error, add paratheses to your statement:Longer:
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:
As you can see, this looks plain wrong. Indeed, JLS §15.26 says this:
Applying right-associativity:
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).
See "Java operator precedence". Meanwhile, use:
Brother, try putting expressions in brackets.
X= (b? 1 : (y=2));
this will work fine.
Java operator precedence is as follows
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
here
ternary
comes beforeassignment
operation. so your statement will be like thisx= ( ternary evaluation )= assignment value
if you still want set value for y , when b is false, you may use
()
for 'y=2' bring inside ternary evaluation.