Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Side effect preincrement and post increment  RSS feed

 
Luca Olivieri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have a question about preincrement/postincrement in Java expression. I know that in Java there is a precedence for operators ((https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html)) so if I write
this expression


int c = 25 - 5 * 4 / 2 - 10 + 4;

the result is 9 because the expression is evaluated in this order int c = 25 - ((5 * 4) / 2) - 10 + 4;

But if i write this expression

int i = 10;

int sum = 12 * i++;

the result is 120.

In the tutorial of Oracle the ++ / - have an order of precedence increased operator *, so I thought the previous expression was evaluated in this mode :

1) increment i (i = 11 also if it's a postincrement)
2) multiplicative (12*11)

Why isn't respect the operator precedence ?

thank you

Luca
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luca Olivieri wrote:
int c = 25 - 5 * 4 / 2 - 10 + 4;

the result is 9 because the expression is evaluated in this order int c = 25 - ((5 * 4) / 2) - 10 + 4;


Please don't confuse precedence with evaluation order -- they are *not* the same thing. You are correct, in that with precedence, it has a result, as if you had placed all those parenthesis in the expression... but ... a computer is not a person. There isn't any natural tendency to evaluate an expression from the inner parenthesis to the outer parenthesis. The evaluation order is defined by the JLS, which specified that it is done left to right.

So, in your example, the "c" is evaluated first, followed by the equals (which determines an assignment), etc. It is just that the actual assignment doesn't happen first, as the RHS of the expression needs to be completed first, but still, the "c" is evaluated first.

Henry
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In reading your question again, I noticed that you are also misunderstanding the post increment operator too.

Luca Olivieri wrote:
int i = 10;

int sum = 12 * i++;

the result is 120.

In the tutorial of Oracle the ++ / - have an order of precedence increased operator *, so I thought the previous expression was evaluated in this mode :

1) increment i (i = 11 also if it's a postincrement)
2) multiplicative (12*11)

Why isn't respect the operator precedence ?


The post increment operator (like any operator) produces a result, when is operates on its operands. In this case, the result of the post increment operator is the value of the variable prior to increment. This would be true, even if it was earlier in the expression (and hence, evaluated earlier).

The precedence doesn't directly make the post increment operator (or any operator) change how it calculates the result. It can only change the operands that are submitted to the operator for evaluation.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!