• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Error in OCA 8 study guide

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In table 2.1 it states Post-unary operators take precedence over Pre-unary operators. Few pages later on page 59 it interprets the statement as "First the x is incremented and returned to the expression" These statements are mutually exclusive so one is wrong. Please explain which one is wrong and give a proper explanation of unary operations.
 
Sheriff
Posts: 4633
300
IntelliJ IDE Clojure Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asked and answered: https://coderanch.com/t/664880/certification/Operator-Precedence-Increment-Decrement-Operators
 
Marshal
Posts: 64707
226
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

As you will find out from the link Tim gave you, and the other links therein, the left‑to‑right rule about evaluation takes precedence.
 
Sam Tzu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I spent a few hours reading those answers. They don't answer my questions. According to the Table 2.1 should evaluate to 2*5/3+1.

I read this but it doesn't cover all the operators and doesn't make much sense https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7

Can someone explain the order of operations when evaluating expressions and methods.
 
Sam Tzu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*int x = 3
int y = ++x * 5 / x-- + --x;
should evaluate to 2*5/3+1. When x-- takes precedence over --x.
 
Campbell Ritchie
Marshal
Posts: 64707
226
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Tzu wrote:. . . should evaluate to 2*5/3+1. . . . .

No, that is inconsistent with the left‑to‑right rule.
4 * 5 / 4 + 2
Which is of course 7.

jshell on my computer wrote:jshell> int i = 3;
i ==> 3

jshell> System.out.println(++i * 5 / i-- + --i);
7

The ++x at the beginning is evaluated before the execution reaches the x-- later.
 
Sam Tzu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Sam Tzu wrote:. . . should evaluate to 2*5/3+1. . . . .

No, that is inconsistent with the left‑to‑right rule.
4 * 5 / 4 + 2
Which is of course 7.

jshell on my computer wrote:jshell> int i = 3;
i ==> 3

jshell> System.out.println(++i * 5 / i-- + --i);
7

The ++x at the beginning is evaluated before the execution reaches the x-- later.



Then why does this 1 + 2 * 3 evaluate to 7?
 
Campbell Ritchie
Marshal
Posts: 64707
226
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Tzu wrote:. . . Then why does this 1 + 2 * 3 evaluate to 7?

Because the precedences are left‑to‑right rule are here consistent with each other.
 
Sam Tzu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
Evaluate the left operand of the ×



So what you're implying is that unary operators are independent of other operators. And the unary operators have no affect on non-unary operators order of execution. And after finding the next non-unary operator statement to be evaluated you then evaluate the left unary operation, then the right unary operation and finally the non-unary operation.

Why does table 2.1 show that x++ has a higher precedence than -x when ++ always executed after the statement, and - always is before.
 
Campbell Ritchie
Marshal
Posts: 64707
226
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Tzu wrote:. . . So what you're implying is that unary operators are independent of other operators.

No.

. . . Why does table 2.1 show that x++ has a higher precedence than -x when ++ always executed after the statement, and - always is before.

Which table 2.1?
No, x++ is not executed after anything. There are three values, that of x before, that of x after, and that of x++. The same applies to ++x, but nobody ever gets confused about that.The new value of x is hidden from you. The value of x++ is equal to the old value of x.
 
Sam Tzu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why does it say post unary operators take precedence over unary operators.

So x equals the after x when the statement completes.
 
Campbell Ritchie
Marshal
Posts: 64707
226
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Tzu wrote:. . . Why does it say post unary operators take precedence over unary operators.  . . . .

Because postfix operators have a higher precedence than prefix unary operators. The problem is not the syntax; the problem is with the semantics of x++. Which I have tried to explain.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!