Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# (++k + ++k * (k3=k++));

Ranch Hand
Posts: 49
can can some one explain the evaluation order of this expr.
since postfix operator has highest precedence
k++ should be evaluated first
therefore k3 should be5
k=5;
System.out.println(++k + ++k * (k3=k++));

Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
Please have a look at the following discussion which explains how the ++ operator works:
http://www.coderanch.com/t/190825/java-programmer-SCJP/certification/Array

Barry Gaunt
Ranch Hand
Posts: 7729
If you bracket the expression according to precedence and grouping rules you get:
((++k) + ((++k) * ( k3 = (k++)))).
Set k=5 and scan from the left evaluating a sub-expression when you get to a closing parenthesis, like this:

So 55 is the value printed, k is 8, and k3 is 7.
[ August 29, 2002: Message edited by: Barry Gaunt ]

Barkat Mardhani
Ranch Hand
Posts: 787
Hi Barry:
++k + ++k * (k3=k++)
I thought that () have highest precedence. So the
expression (k3=k++) should be evaluated first. Right?
Thanks
Barkat

Barry Gaunt
Ranch Hand
Posts: 7729
If you add a couple of println statements to print k and k3 you will get the results I gave.
Putting the parentheses around the k3=k++ does not imply that the sub-expression is evaluated first.
The (k3=k++) is taken as the second term for the multiplication, that's all.
If those parentheses were not there the expression would not compile, because the "=" has lowest precedence.
I hope a Guru will see this and confirm my analysis.
-Cheers
Barry
BTW take a look at Marcus Green's JCHQ where I analysed some other expressions in the same way.
[ August 29, 2002: Message edited by: Barry Gaunt ]

Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
http://www.coderanch.com/t/239100/java-programmer-SCJP/certification/operator-precedence
In brief, () is not an operator but a separator, so it always has the highest precedence over operator. But as Barry said, it doesn't mean that the ()-expression will be evaluated first. It is juts a means of structuring the expression in a more readable way.

Barry Gaunt
Ranch Hand
Posts: 7729
BTW. When I analyse the expression ++k + ++k * (k3=k++), applying the precedence rules I treat the parentheses as "user-given", I cannot (must not) remove them.
-Barry

Ranch Hand
Posts: 49
well i odes not give compile error.
and j does .
that's the truth
but i don't knowhy.

Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
I'm pretty sure your answer was meant to be posted in the "scope of variable" discussion...
[ August 29, 2002: Message edited by: Valentin Crettaz ]

Barry Gaunt
Ranch Hand
Posts: 7729
I'm sure that is one of the symptoms of JavaRanch Junkie Syndrome (see MD). But after only 9 posts ?

[ August 30, 2002: Message edited by: Barry Gaunt ]

Ranch Hand
Posts: 49
yeah.
my mistake.sorry for that.
meant to post in scope guestion but both windows were open

Barkat Mardhani
Ranch Hand
Posts: 787
Hi Val:

In brief, () is not an operator but a separator, so it always has the highest precedence over operator. But as Barry said, it doesn't mean that the ()-expression will be evaluated first. It is juts a means of structuring the expression in a more readable way.

Is it not the fact that in other languages (X base for example) that () have highest precedence and are evaluated first? I love Java untill it starts to trick me with subtle difference between separater and operator...
Thanks
Barkat
[ August 30, 2002: Message edited by: Barkat Mardhani ]