*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes (++k + ++k * (k3=k++)); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "(++k + ++k * (k3=k++));" Watch "(++k + ++k * (k3=k++));" New topic
Author

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

madhur jain
Ranch Hand

Joined: Aug 27, 2002
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
but the answer is 7.
k=5;
System.out.println(++k + ++k * (k3=k++));
madhur.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
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

Joined: Aug 03, 2002
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

Joined: Aug 26, 2001
Posts: 7610
PLease see the following discussion:
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

Joined: Aug 03, 2002
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
madhur jain
Ranch Hand

Joined: Aug 27, 2002
Posts: 49
well i odes not give compile error.
and j does .
that's the truth
but i don't knowhy.
madhur.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
madhur, what i and j are you talking about?
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

Joined: Aug 03, 2002
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 ]
madhur jain
Ranch Hand

Joined: Aug 27, 2002
Posts: 49
yeah.
my mistake.sorry for that.
meant to post in scope guestion but both windows were open
madhur.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: (++k + ++k * (k3=k++));
 
Similar Threads
Getting custom key from map based on comparator etc.
++k + k++ + + k
Triple DES with Cipher Block Chaining - Beginner que
Confused with pre and Post increment please help ..
Regarding multiple IF's using "OR"