Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Operator precedance example (Java OCA 8 Programmer I Study Guide, Sybex)

 
Ranch Hand
Posts: 75
Eclipse IDE AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


According to the operator precedance, the post fix operator should be evaluated first but in the solution the first prefix is evaluated first.
What is the correct output?
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Saumyaraj Zala wrote:
According to the operator precedance, the post fix operator should be evaluated first but in the solution the first prefix is evaluated first.



Don't confuse precedence with the order of evaluation. The precedence determines which operator takes precedence in an expression, but it doesn't determine how the expression is evaluated. For the most part, the JLS states that expressions are evaluated from left to right.

As a simpler example, for ...

2 + 3 * 4

precedence states that it should be ...

2 + (3 * 4)

but while a person may do the multiply first, Java does not. The expression is still done left to right, using temporary variables as needed.

Henry
 
MyExamCloud Software Support
Posts: 692
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct answer is 7.
Here is how it is evaluated:

4*5/4+2  = 7


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

 
Saumyaraj Zala
Ranch Hand
Posts: 75
Eclipse IDE AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output gets changed with precedence right?
If we ignore the precedence of postfix operator over prefix operator and start by left to right it can result in wrong output.
I dont know what i am missing here. evaluation from left to right will only happen if the precedance is same right?
What should be the output of the above expression ?
 
Saumyaraj Zala
Ranch Hand
Posts: 75
Eclipse IDE AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jhonson Fernando wrote:The correct answer is 7.
Here is how it is evaluated:

4*5/4+2  = 7


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


Why do we increment x first resulting in 4. Aint it in prefix operator?
 
Saumyaraj Zala
Ranch Hand
Posts: 75
Eclipse IDE AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jhonson Fernando wrote:The correct answer is 7.
Here is how it is evaluated:

4*5/4+2  = 7


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



According to me it should be :
int y = ++x * 5/3 + --x;
int y = 3*5/3+ --x;
int y = 3*5/3+2;

but in book it is your way.
 
Saloon Keeper
Posts: 12165
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Precedence has nothing to do with evaluation order. Operator precedence only determines which operands go with which operators.

When you have ++x++, precedence determines that the expression is evaluated as ++(x++). You can imagine this as if the compiler explicitly inserts parentheses. After the parentheses have been inserted, precedence doesn't matter anymore, and the expression is evaluated from left to right.

Here's what precedence does in the expression you posted:

Then, at runtime, ++x happens before x--, because it's on the left.
 
Stephan van Hulst
Saloon Keeper
Posts: 12165
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my example above, ++(x++) is not valid code, because the prefix operator needs to be applied to a variable instead of another expression type, but I hope you understand my example.
 
Ranch Hand
Posts: 135
5
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, it would be something like this at the execution time

int x = 3;

int y = 4 * 5 / x-- {x = 3 + 1} + --x;
int y = 20 / x-- + --x;
int y = 20 / 4  {because post increment, we took the value as 4, but newest x = 4 - 1}+ --x;
int y = 5 + --x {because pre-increment, x = 3 - 1};
int y = 5 + 2;
int y = 7;

So if we have something like this,
int x = 3;
int y = x-- + x-- + x--;
int y = 3 + {current x = 2}x-- + x--;
int y = 3 + 2 + {new x = 1}x--;
int y = 3 + 2 + 1;

 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
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
    Bookmark Topic Watch Topic
  • New Topic