This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operator confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operator confusion" Watch "operator confusion" New topic

operator confusion

Gary Laird

Joined: Mar 26, 2004
Posts: 3
Could someone please help me on this?

Heres the question, taken from Page 65 of Mughal and Rasmussen Book

int k = 1;
int i = ++k + k++ + + k;

Their evaluation is ((++k) + (k++)) + (+k) -> ((2) + (2) + (3))) = 7

Now on page 42, they give you the operator precedence table

Postfix Operators [] . parameters exp++ exp--
Unary Prefix operators ++exp --exp +exp -exp ~ !

To be, that means that the 2nd k in the expression should be evaluated first.(k++) giving a value of 1.

What am I missing here?

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Try reading this and this.

SCJP Tipline, etc.
Gary Laird

Joined: Mar 26, 2004
Posts: 3
nice one thanks
I agree. Here's the link:
subject: operator confusion
Similar Threads
question from Khalid book
Operator precedence
Operator Precendence
Operators - Mughal p.56 Ques 3.7
Operator precedence