aspose file tools*
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
Author

operator precedence

d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76
Hi,

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é
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Same as this :
http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment


[My Blog]
All roads lead to JavaRanch
kwan Jang
Greenhorn

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: operator precedence