• Post Reply Bookmark Topic Watch Topic
  • New Topic

operator precedence  RSS feed

 
Dani Garcia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to ask if somebody can clarify me the output of the followed codes.

**********

1. since pos increment has precedence over preincrement, i++ is evaluted first, so the expresion is "++i - y + 9"
2. i was incremented and since preincrement has precende over the rest operators, the expresion is "11 - y + 9 "
3. so the final expresion would "11 - 2 + 9" = 18, that is what I expect

**********
but I dont understand this output


1. for me, since postincrement has precedence over preincrement, i++ is evaluted first so the expresion is "++i * y + 9"
2. i was incremented and since preincrement has precende over the rest operators, the expresion is "11 * y + 9 "
3. so the final expresion would be "11 * 2 + 9" = 31, but it is 30 can somebody clarify why this?

Thanks.


 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

The fact that post-increment has precedence over pre-increment has no bearing here. It's more to do with left to right evaluation of expressions. The expression evaluates to 10 - 2 + 10, not 11 - 2 + 9. Unfortunately, both are equal to 18 which led you to think you were correct in thinking how it evaluated.

Likewise, the second expression evaluates to 10 * 2 + 10, which of course is 30 not 31.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In both your #1 points, your interpretation of how operator precedence works is incorrect. Think of operator precedence as putting explicit parentheses around the terms of an expression. After explicit parentheses have been added, evaluation of the expression still proceeds from left to right. The effects of pre-increment operations are applied as you go. So, in the first pre-increment of the variable i, its value becomes 10. When you get to the second i with post-increment, the value 10 is used, not 9.
 
Dani Garcia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:In both your #1 points, your interpretation of how operator precedence works is incorrect. Think of operator precedence as putting explicit parentheses around the terms of an expression. After explicit parentheses have been added, evaluation of the expression still proceeds from left to right. The effects of pre-increment operations are applied as you go. So, in the first pre-increment of the variable i, its value becomes 10. When you get to the second i with post-increment, the value 10 is used, not 9.


Thanks for your answer Junilu. I thought postincrement has precende over preincrement because under https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html postfix is before unary. Am I interpreting this table wrong?
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are misinterpreting it. The precedence applies to operator evaluation, not expression evaluation. Consider this expression:

1 + 2 * 3

This expression is evaluated as 1 + (2 * 3), which results in the value 7, not 9. Now, if we had this code:

The above code will print out 7 because it evaluates to 1 + 2 * 3. So, even though unary pre-increment has a higher precedence over * which has a higher precedence over +, the evaluation still proceeds from left to right and the pre-incrementing of i happens as the variable i is used throughout the expression. I hope that makes sense.
 
Dani Garcia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! Thanks for your aclaration. It has made me to see the difference. Now I understand what you meant with the brackets. I have been reading about expresion and operators, and I found a comment that helped me too.


Consider A() + B() + C() * D(). Multiplication is higher precedence than addition, and addition is left-associative, so this is equivalent to (A() + B()) + (C() * D()) But knowing that only tells you that the first addition will happen before the second addition, and that the multiplication will happen before the second addition. It does not tell you in what order A(), B(), C() and D() will be called!
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can have a great understanding of expressions(even other things)→here.

Kind regards,
praveen.
 
Dani Garcia
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for that resource..it is wonderful!!!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Consider this expression:

1 + 2 * 3

This expression is evaluated as 1 + (2 * 3), which results in the value 7, not 9.


I think the other issue is human nature -- as the reason why many of us confuse precedence with evaluation order. It is in our nature to work "outward" when evaluating an expression like "1 + (2 * 3)", ie. doing the multiplication first.  People forget that computers are different, and tend to do stuff based on how they are programmed (and specified) to do it.

And with Java, expressions are done left to right (using temporary/stack variables when needed).

Henry
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dani Garcia wrote:thanks for that resource..it is wonderful!!!
It is good; there are a few tiny errors, e.g. saying that + is overloaded when both its operands are String type. Since the article mentions the Math class' exact methods, it appears to be reasonably recent.
We usually think the way Henry describes because that is how we were taught maths at school. Years of having BoDMAS drummed into us.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!