• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 727
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14507
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14507
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;

 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Already discussed here --->  https://coderanch.com/t/664880/certification/Operator-Precedence-Increment-Decrement-Operators   worth reading.
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic