Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Operator Precedence Error OCA SE8 Ch 2 Pg 59

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I either found an error in the OCA SE 8 Study Guide book for just don't understand it.

In Ch 2 pg 59 there is the following example snippet:



The explanation of the result is as follows.



First, the x is incremented and returned to the expression, which is multiplied by 5. We can simplify this:



Next, x is decremented, but the original value of 4 is used in the expression, leading to



The final assignment of x reduces the value to 2, and since this is a pre-increment operator, that value is returned to the expression:



Finally, we evaluate the multiple and division from left-to-right ....



The explanation does arrive at the correct values for x and y but, I think the order of ops is described incorrect. Shouldn't it be that the Post-unary operators are evaluated first (x--) as described in "Table 2.1 Order of operator precedence" on pages 52-53 which in this case is as follows below?


 
Marshal
Posts: 68888
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It is not simply a case of precedences, but the fact that all operations are evaluated left to right. The leftmost operator (preincrement) is applied to x first. The fact that postincrement has a higher precedence does not mean that x-- after the divide sign is evaluated first. The left to right rule takes precedence. The JLS (Java┬« Language Specification) tells you that the left operand is evaluated first, so the x-- token will have the value 4.

Please explain what your line 5 means; I think it is mistaken.
 
Saloon Keeper
Posts: 11879
253
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

No. Operator precedence does not determine order of evaluation. Operator precedence only determines which operand goes with which operator. You can imagine it as the compiler inserting explicit parentheses in the expression:

After it's done this, operator precedence and associativity no longer play a role. Evaluation is ALWAYS left-to-right.

Consider the expression -x++. The higher precedence of unary postfix operators as compared to unary prefix operators ensures that this expression is interpreted as -(x++), and not as (-x)++.
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic