• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Possible errata OCP Java SE 11 study guide - Precedence query

 
Greenhorn
Posts: 11
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On page 85, regarding the code involving the lion, Line 1 in the 2nd paragraph reads, "First, lion is incremented and then returned to the expression, ", following which, it is mentioned that lion is decremented.

Is this an incorrect interpretation of the precedence of the post and pre-unary operators, or is the precedence of these operators ignored when they appear in the same line of arithmetic code and the compiler reads from left to right anyway?

xoxoxo
 
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain more about your question. I think you may have gone down with the common misunderstanding that precedences of operators equate to order of execution.
 
author & internet detective
Posts: 39996
807
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code being discussed here is:


On page 81, we have the order of precedence table. Where we note that x++ is a higher precedence than --x.

So you are correct that the explanation should use that order. Good catch. I can't believe nobody noticed that in the OCA 8 book!
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you get an overflow error, the answer should always be 5.
 
Jeanne Boyarsky
author & internet detective
Posts: 39996
807
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually this is not an errata. Just removed it from the list.

The table on page 81 says expression++ and expression -- are both higher precedence than ++expression. So ++lion does get evaluated second.
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Actually this is not an errata. . . .

Agree: the original statement was correct.

. . . So ++lion does get evaluated second.

No, ++lion is evaluated before the multiplication, because of the left to right rule. If you manage not to get overflow, you get 5 as the result:-

Campbell's JShell wrote:jshell> int lion = new Random().nextInt(); int tiger = ++lion *  5 / lion--;
lion ==> -306843420
tiger ==> 5

You can see that the increment to 0 precedes the post‑decrement here:-

Campbell's JShell wrote:jshell> int lion = -1; int tiger = ++lion *  5 / lion--;
lion ==> -1
|  Exception java.lang.ArithmeticException: / by zero
|        at (#12:1)

The increment from −1 to 0 occurs before the division and after the post‑decrement, the value of lion-- remains 0, hence the exception. Otherwise the expression would evaluate to *5/−2, i.e. 0.
 
Jeanne Boyarsky
author & internet detective
Posts: 39996
807
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And back to errata.

The moral of the story is:
1) The "post" operators execute before the "pre" operators
2) This example is too complicated, even for the exam
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you really sure about this erratum? In the original question, the order in which lion-- and ++lion were executed did not matter for the final result, since that was always 5. However, if you change lion-- into lion++, you can check how the expression is evaluated.



And the result is:



So it seems the explanation in the book is correct after all, but I'm inclined to agree that this example is too complex.
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Yes, as we concluded earlier, the original explanation in the book appears to be correct.
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic