This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Post- and Pre- unary operator precedence (Java OCA 8 Programmer I Study Guide)

 
Alan Couze
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In chapter 2 on page 57 there is an example on the pre/post increment and decrement operators:



It explains that first, the x is incremented and returned:


However, on page 51, Table 2.1 it says that post-unary operators have higher precedence over pre-unary operators. So in this example why wouldn't the x be decremented to 2 first and return 3?



Then x would be incremented to 3



...and then x would be decremented back to 2:







This yields the same result that the book states, but just in a different manner. Which is correct?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Couze wrote:This yields the same result that the book states, but just in a different manner. Which is correct?

This topic has a great explanation about the same code snippet. If you still have doubts after reading this topic, let us know by simply hitting the "Post Reply" button

Hope it helps!
Kind regards,
Roel
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it comes to pre- and post increment/decrement, the assign first or increment/decrement first needs to be clear.

Post increment / decrement (x++) is assign then increment/decrement

Pre increment / decrement (--x) is increment/ decrement then assign

Both explanations are correct. Either approach will get you the same result.

Using the rule above, working from left to right applying the post-unary or pre-unary all at once is probably the straightforward way, despite the post unary has higher precedence.

y = ++x * 5 / x-- + --x

using left to right post and pre unary apply all at once
=> 4 * 5 / 3 + 1
=> 7

or using post unary first
=> 3 * 5 / 3 + 2
=> 7
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Both explanations are correct. Either approach will get you the same result.

That's absolutely NOT true! Expressions are evaluated from left to right, so post-unary operator is not executed first!

K. Tsang wrote:using left to right post and pre unary apply all at once
=> 4 * 5 / 3 + 1
=> 7

Those values are incorrect. Should be:
=> 4 * 5 / 4 + 2
=> 7
(the value of x will never be 1)

K. Tsang wrote:or using post unary first
=> 3 * 5 / 3 + 2
=> 7

And this is completely wrong as the post-unary operator is not executed first at all (as mentioned before).

With any other expression using post- and pre-unary operators, the result will not be the same. So you can't conclude that both explanations are correct just because for one expression the results are the same. Here is an example that disproves both explanations being correct!1/ Expression evaluated from left to right (the correct one): int j1 = 6 * 6 * 5; // i1 = 4, j1 = 180
2/ Post-unary operators evaluated first (the wrong one): int j1 = 4 * 5 * 4; // i1 = 4, j1 = 80

Hope it helps!
Kind regards,
Roel
 
Liutauras Vilda
Bartender
Pie
Posts: 2792
112
BSD VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan,

You always should try in practice after you read, so you could check if you're right. How to check?

Here is how I think in my head about them ("++x", "x--", "--x"), like about a standalone expressions.

At the beginning you have x = 3.

1. x = 3

2. Since it is pre-increment, you increment "x" first (3+1), only then you place it in expression
int y = 4 * 5 / ...

3. As you know, your "x" is 4 now. Next step you have post-decrement (x--). So you first place your current "x" value to expression, then decrement.
int y = 4 * 5 / 4 + ...

4. You know from previous step. That your "x" after was placed to expression, then had to be decremented (4 - 1 = 3). So the last one you have is "--x". And current "x" is 3. So again, decrement first again, and only then place to expression.
int y = 4 * 5 / 4 + 2

5. And, since first multiplication/division, then addition, you should think about it as:
int y = ((4 * 5) / 4) + 2

So you have:
int y = 20 / 4 + 2;
int y = 5 + 2;
int y = 7;

Hope it helps a bit.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something worth noting is this kind of thing:

It will print 0.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:Something worth noting is this kind of thing:

It will print 0.

Indeed! And if you are looking for a detailed explanation why it prints 0, look no further, just click here
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic