File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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
It's not a secret anymore!