I came across this question on an exam.
Exam question: What is the result of the following code snippet?
3: var tiger = "Tiger";
4: var lion = "Lion";
5: final var statement = 250 > 338 ? lion : tiger = " is Bigger";
6: System.out.println(statement);
The correct answer was
F. The code will not compile because of line 5
The explanation is:
- The code does not compile because the assignment operator has the highest order of precedence in this expression.
- Both sides of the ternary operator must have the same type. This expression is invalid, as the left side of the second assignment operator is not a variable, so the answer is option F.
- Note that if the question had added explicit parentheses around the expression (Tiger = " is Bigger"), option E would have the correct output.
When I ran the code myself, I got a compilation error:
test.java:11: error: unexpected type
final var statement = 250 > 338 ? lion : tiger = " is Bigger";
^
required: variable
found: value
1 error
error: compilation failed
After asking for second opinions, reading JLS section 15 , and these other SO questions:
What are the rules for evaluation order in Java?
If parenthesis has a higher precedence then why is increment operator solved first?
I came up with a few theories:
With the new knowledge, I will now try to explain why line #5 fail to compile:
250 > 338? lion: tiger
250 > 338? lion: tiger
, which is a valid expression, but this expression evaluates to a value. Now I would try to apply this same theory to explain the correct scenario of this code: final var statement = 250 > 338? lion: (tiger = " is Bigger");
final var statement = 250 > 338? lion: (tiger = " is Bigger");
Has the explanation provided by the exam dropped the ball or do I still not understanding how this code did not compile?
You are correct, they misspoke, assignment operator has the lowest order of precedence.
You are incorrect, they never mention "order of evaluation " anywhere (that you've shown, anyway). The code shown doesn't do anything where order of evaluation matters. The assignment has nothing to with order of evaluation.
- The code does not compile because the assignment operator has the highest order of precedence in this expression.
Operator precedence shows:
9 > relational
2 ?: ternary
1 = assignment
Which means that to explicitly show precedence using parenthesis, the statement becomes:
statement = ((250 > 338) ? lion : tiger) = " is Bigger";
- Both sides of the ternary operator must have the same type. This expression is invalid, as the left side of the second assignment operator is not a variable, so the answer is option F.
The ternary operator being ((250 > 338)? lion: tiger)
, "both sides" refer to the two assignment operators.
As it says, "This expression is invalid, as the left side of the second assignment operator is not a variable".
- Note that if the question had added explicit parentheses around the expression (Tiger = " is Bigger"), option E would have the correct output.
You already confirmed that yourself.
To explicitly show precedence using parenthesis, the statement becomes:
statement = ( (250 > 338) ? lion : (tiger = " is Bigger") );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.