• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Saumyaraj Zala
Ranch Hand
Posts: 75
AngularJS Eclipse IDE 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?
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
 
Jhonson Fernando
MyExamCloud Software Support
Ranch Hand
Posts: 680
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
AngularJS Eclipse IDE 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
AngularJS Eclipse IDE 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
AngularJS Eclipse IDE 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.
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • 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
Bartender
Pie
Posts: 6503
83
  • 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.
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • 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;

 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Already discussed here --->  https://coderanch.com/t/664880/certification/Operator-Precedence-Increment-Decrement-Operators  ; worth reading.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic