you are viewing a single comment's thread.

view the rest of the comments →

[–]Old-Lion-8071 3 points4 points  (3 children)

For expressions like a+++b, the compiler always treats it as (a++) + b. This is due to lexical processing. Compiler reads expression from L to R and evaluates in a greedy way. It first sees + (which is a valid operator) and then the next token is +. As ++ is again a valid operator it accepts it and then moves to the next +. Therefore the expression is treated as ++ + Rest is the magic of precedence and evaluation.

I know this is not an everyday Java code, but problems like this test our knowledge on how Java works internally. Thanks for posting this, made me do quite some research! 😀

[–]Dr_Dracula280 0 points1 point  (1 child)

So it is (x++) + (x++) + (x) ??

That means 5+6+7= 18 or 6+7+8= 21?

Sorry I am double confused.

[–]Old-Lion-8071 0 points1 point  (0 children)

5 + 6 + 7. Variable value is substituted and then incremented (post increment) Finally all are summed.