Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Help with a question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OCP Java SE11 Complete study guide, site 86 is following Question:



i thought the post-decrement would change the value after the calculation, like this:
1. ++lion
2. 4 * 5 / 4
3. lion--
after getting the result (2), i would decrement lion.

But in the explanation they do the post-decrement first. I cant understand why..
1. lion--
2. ++lion
3. 3 * 5 / 3

Can someone explain me this?
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

according to the java operator precedence rules, the postfix operator is evaluated first.
A useful page : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
 
Saloon Keeper
Posts: 12893
280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Zotz wrote:i thought the post-decrement would change the value after the calculation, like this:
1. ++lion
2. 4 * 5 / 4
3. lion--


This is correct. The application prints "lion is 3" and "tiger is 5".

But in the explanation they do the post-decrement first.


The explanation is wrong.


Marc Fiore wrote:according to the java operator precedence rules, the postfix operator is evaluated first.


Incorrect. Operator precedence has nothing to do with order of evaluation. The only thing that operator precedence determines is which operand goes with which operator. It's as if the compiler inserts parentheses in the expression:

After this has been done, the expression is evaluated left to right, so (++lion * 5) is evaluated before (lion--).
 
Enthuware Software Support
Posts: 4540
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to check out this excerpt from OCP Java 11 Part 1 Fundamentals.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Zotz wrote:OCP Java SE11 Complete study guide, site 86 is following Question:



i thought the post-decrement would change the value after the calculation, like this:
1. ++lion
2. 4 * 5 / 4
3. lion--
after getting the result (2), i would decrement lion.

But in the explanation they do the post-decrement first. I cant understand why..
1. lion--
2. ++lion
3. 3 * 5 / 3

Can someone explain me this?



In the numerator ++ has higher precedence so it increases the value of lion to 4, then multiplies. In the denominator, it divides with the incremented value of lion first and then decrements back to 4. Try changing it to --lion in the denominator;

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic