Win a copy of Terraform in Action this week in the Cloud 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Possible errata OCP Java SE 11 study guide - Precedence query

 
Greenhorn
Posts: 11
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 40853
831
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless you get an overflow error, the answer should always be 5.
 
Jeanne Boyarsky
author & internet detective
Posts: 40853
831
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 40853
831
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Yes, as we concluded earlier, the original explanation in the book appears to be correct.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if I am beating a dead moose here, but in my edition of the book this is still discussed incorrectly




This one is more complicated than the previous example because lion is modified two times on the same line. Each time it is modified, the value of lion changes, with different values being assigned to the variable. As you’ll recall from our discussion on operator precedence, order of operation plays an important part in evaluating this example. So how do you read this code?
First, lion is decremented. We can simplify this: Next, lion is incremented with the new value of 3 used in the expression, leading to this:Finally, we evaluate multiplication and division from left to right. The product of the first two numbers is 15. The divisor 3 divides 15 evenly, resulting in an assignment of 5 to tiger. The result is then printed:



Boyarsky, Jeanne. OCP Oracle Certified Professional Java SE 11 Developer Complete Study Guide (pp. 86-87). Wiley. Kindle Edition.
 
Jeanne Boyarsky
author & internet detective
Posts: 40853
831
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've added it back to the errata. Scott and I discussed it and we want to get rid of this example. it's too complicated for the exam. It's also too complicated to explain/understand to be useful.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
if the postfix precedence (expr--, expr++) is greater than the prefix one (++expr, --expr), as shown in table 3.1 (OCP, Java SE 11, COMPLETE STUDY GUIDE, 2020 edition, pag. 83... the "lion" 's example is now at page 86), why after the following istructions the value of result is zero?

int result, num = 9;  
result = --num - num--;
System.out.println("num="+num+" result=" + result); //num=7 result=0

It seems that the --num espression is executed before the num-- expression...

Thanks in advance to your answers.
 
Campbell Ritchie
Marshal
Posts: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

The lion and tiger example shows that many people don't know the principal rule about execution order: eva;luate everything as execution going left to right. Because the predecrement operator has a higher precedence than subtraction, it is executed before the subtraction, and then the execution goes to the right and the post decrement is executed.
--i − i-- evaluates to 0 for all starting values of. i.
 
Campbell Ritchie
Marshal
Posts: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:. . . this example. [is] it's too complicated for the exam. . . . .

...but so many people get it wrong. You need something to demonstrate that the left to right rule takes priority over precedences.
 
Arnaldo Bianchi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell Ritchie,
thanks for your answer. But, about the lion and the tiger example, you can read in the book mentioned above, at page 87:
"First, lion is decremented. We can simplify this:
int tiger = ++lion * 5 / 3; // lion assigned value of 2"

It seems that is wrong. The expression ( --num - num--) mentioned above demonstrates that prefix and postfix have the same precedence level (so postifix precedence is not greater than the prefix one) and when they are into the same expression the leftmost operand will be executed first...

Regarsd.
 
Campbell Ritchie
Marshal
Posts: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No postfix operators have a higher precedence than prefix. You can't mix ++i with i++, but you can mix -i and i++. So the higher precedence of postfix operators does make a difference.
The explanation that the decrement occurs first isn't correct. The expression should evaluate to 5.
Please search; the lion and tiger example comes up repeatedly in this forum.
 
Campbell Ritchie
Marshal
Posts: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Earlier today, I wrote:No postfix operators have a higher precedence than prefix. . . ..

I missed out a comma. That should read, “No, postfix operators have a higher precedence than prefix.”
 
Jeanne Boyarsky
author & internet detective
Posts: 40853
831
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Jeanne Boyarsky wrote:. . . this example. [is] it's too complicated for the exam. . . . .

...but so many people get it wrong. You need something to demonstrate that the left to right rule takes priority over precedences.


Agreed. We could demonstrate it with something less confusing though.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic