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 precedence 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 precedence" Watch "operator precedence" New topic

operator precedence

d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76

In operator precedence the postfix operator apparently has a higher priority than the additive operators. However, if we have:

int c = 10;
int d = 5 + c++;
d equates to 15 - which is what I would expect because the postfix operator is only applied when the entire expression has been resolved.

I don't understand why then the postfix operators come before the additive operators on the operator precedence list.

Could anyone explain?

Many Thanks
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

The problem here is that c++ is evaluated first, but it returns the value of c before begin incremented.
int d = 5 + (c++); // 5 + 10
int d = 5 + (++c); // 5 + 15

Same as this :

[My Blog]
All roads lead to JavaRanch
kwan Jang

Joined: Sep 24, 2006
Posts: 16
int d = 5 + (++c); // 5 + 15?
not int d = 5 + (++c); // 5 + 11 ???
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Typo. Thanks for pointing it out kwan.
Chetan Raju
Ranch Hand

Joined: Aug 02, 2006
Posts: 109
Although increment operators come higher up in the hierarchy of precendence table, the order of evaluation also matters. for unary increment operators, I suppose the order of evaluation is from Right to Left.
I agree. Here's the link:
subject: operator precedence
It's not a secret anymore!