Win a copy of TensorFlow 2.0 in Action 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Sybex - Chapter 2 - Increment and Decrement Operators Page 59 - Operators example

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have a question regarding increment and decrement operators section of Chapter 2 in Sybex OCA Java SE 8 Programmer I book.

There is a practice regarding increment and decrement operators:
int x= 3;
int y = ++x * 5 / x-- + --x;
System.out.println("x is " + x);
System.out.println("y is " + y);

The book starts to evaluate "++x" at the beginning.

But according to order of operation in Java, it should evaluate x-- firstly. Right?
 
Marshal
Posts: 25961
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ilkay Polat wrote:But according to order of operation in Java, it should evaluate x-- firstly. Right?



No. Evaluation of expressions always proceeds from left to right.

If you want the gory details (and you probably should if you're writing a certification exam), here's a version of the language specification: Expressions. The part about evaluation order is section 15.7.

Where it says:

The JLS wrote:The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.



This is what you need to know. It also says:

JLS wrote:It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation...



Which you'll notice, the example you're discussing seriously violates (on purpose) this recommendation.
 
Marshal
Posts: 70684
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Paul has explained, the book is correct. Please search; this example has come up many times in this forum and it has confused just about everybody who read it.
It is not a case of “probably” wanting the gory details for the exam, but a case of definitely wanting the gory details. Be sure to look up the link Paul gave you.
 
ilkay Polat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Chapter 2, There is table which shows order of operator precedence in Java.

According to that table, post-unary operators(expression++ or expression--) have high priority than pre-unary operators(++expression or --expression).


So if we follow this order of operator precedence, post-unary operators should be evaluated first within a given expression.


Besides Java guarantees left-to-right evaluation if and only if the operators have the same level of precedence.


So this is my reasoning for this issue.
 
ilkay Polat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also what do you think Jeanne Boyarsky?
 
Campbell Ritchie
Marshal
Posts: 70684
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ilkay Polat wrote:. . . So this is my reasoning for this issue.

Unfortunately it is incorrect.
 
author & internet detective
Posts: 40241
819
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ilkay Polat wrote:Also what do you think Jeanne Boyarsky?


Yes, the book is correct. Items on the same line of table 2.1 are evaluated at the same "level" (so left to right.)

This is clearer with an example like x - y + z. X-y is executed first even though + happens to be listed before - on the same row of table 2.1
 
Greenhorn
Posts: 7
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Higher Precedence does not mean that the operation must be evulated first. It means that before you can evulate  ++x * 5, you have to evulate the ++x

So these are the steps



 
Campbell Ritchie
Marshal
Posts: 70684
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and what about 5 * ++x...? That should give the same result.
 
Marco Ulrich
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what?
 
ilkay Polat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here I attached the table first two lines regarding order of operator precedence.

Post-unary operators and pre-unary operators are in different lines.
First post-unary operators is coming and then you see pre-unary operators on the second line.
Ads-z.png
[Thumbnail for Ads-z.png]
 
Paul Clapham
Marshal
Posts: 25961
70
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marco Ulrich wrote:Higher Precedence does not mean that the operation must be evulated first. It means that before you can evulate  ++x * 5, you have to evulate the ++x



This is the key point. When thinking about order of evaluation, don't get distracted by order of precedence.
 
ilkay Polat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my example:

int x = 4 + 3 * 5;
System.out.println(x);

It prints out 19 instead of 35.

By choosing which operation to perform first, we are actually choosing between two different expressions:
1. (4 + 3) * 5 == 35
2. 4 + (3 * 5) == 19

In this case * has more priority than + in Java so second option is valid. So not always from left to right.
 
Jeanne Boyarsky
author & internet detective
Posts: 40241
819
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* and + aren't at the same level of precedence though.
 
ilkay Polat
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right, Jeanne.

Same applies for post-unary and pre-unary operators. These operators are not at the same level of precedence according to table 2.1 in the book. Is this right?

 
Campbell Ritchie
Marshal
Posts: 70684
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,of course they are at different levels of precedence. But the left to right rule means the ++x is evaluated first.
 
Marco Ulrich
Greenhorn
Posts: 7
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe that way will helps you

You have :

int y = ++x * 5 / x-- + --x;

now you put according to the level of priority step by step every expression in brackets

1. int y = ++x * 5 / (x-- )+ --x
2  int y =( ++x ) * 5 /( x-- )+(--x)
3. int y =( ( ++x ) * 5 ) /( x-- )+(--x;)
4. int y = ( ( ( ++x ) * 5 ) /( x-- ) ) +(--x;)

Now you can evulate it from left to right, if there is a bracket you have to evulate the contant first
 
Paul Clapham
Marshal
Posts: 25961
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ilkay Polat wrote:By choosing which operation to perform first, we are actually choosing between two different expressions:
1. (4 + 3) * 5 == 35
2. 4 + (3 * 5) == 19

In this case * has more priority than + in Java so second option is valid. So not always from left to right.



Indeed, this is all correct -- except for the last statement. Well, the last statement is correct if you're talking about the order in which the operators are applied, but that isn't the order in which the operands are evaluated. If you replace 4, 3, and 5 by expressions which have side effects (like printing their values on the console) then you'll see that 4 is evaluated first, then 3, then 5.

Read Marco's most recent post for an excellent description of how the expression in the exam works.
 
Normally trees don't drive trucks. Does this tiny ad have a license?
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