Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Did not get this either!!

 
Karthik Subramanian
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello Friends,
This Question is from Test your java Knowledge exam

Question 9:
Determine the result of attempting to compile and run the following code:
public class Tester {
static int x;
public static void main(String[] args) {
int i = 1;
int j = (i + (i = 4)) * (i = 3);//
System.out.println(j);
int y = z() * (z() + z());
System.out.println(y);
}
private static int z() {
return ++x;
}
}
Answer 9:
The output is:
15
5

My doubt is the evalution of the expression at line //.
Now in that marked expression the evalution occur from
left to right,but because (i = 4) is put in braces shouldn't it
be evaluated first.Doesn't the () have the highest priority??
But it is not happening here.
Could somebody explain??
Thanks a million
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kartik,
To help you understand the order of evaluation, I took a part of your code and wrote the program below.

Don't worry about too many nested calls to 'Dump()'. The idea here is to probe the intermediate results of the expression so that the order of invocation of Dump method, which is traced by the println statements gives you the order of evaluation of the expression.
When I ran this code, I got this output -

1
4
5
3
15

Clearly, the expression
int j = (i + (i = 4)) * (i = 3);
is evaluated in the following order.
Step I - Evaluate (i + (i = 4))
Step II - Evaluate (i = 3)
Step III - Evaluate (Step I) * ( Step III )
For Step I -
a) Evaluate i
b) Evaluate i + 1
c) Evaluate (a)+(b)
Hope that helps. Infact you can use this simple technique to " spy on Java's path " when it evaluates complex expressions. Hope that helps.....if not, come back for more
Ajith
[This message has been edited by Ajith Kallambella (edited August 31, 2000).]
 
Karthik Subramanian
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dear Ajith,
I am really thankful to you for pondering over this
Question.
But the concept is not crystal clear to me.
You had taken the help of function calling
technique to evaluate this.
But i have seen somewhere that if a piece of code
looks like this,
int a = func(i) * (i = i +2 (3 * 2));
then the func(i) is evaluated first ,even if we add
half a dozen () around the second part i,e
(i +2 (3 * 2)).

This is where i feel let down.
What i have learnt is that in any arithmetic expression irrespective of whether the
operators existing in the expression work
from left to right or from right to left,
the first part of the expression that gets to be
evaluated is the one that is put inside brackets()
From this understanding if i evaluate this expression,
int j = (i + (i = 4)) * (i = 3);
According to me ,
First Evaluation : (i = 4)//Now i is four and not 1
Second Evaluation : ( i + 4))// i, e (4 + 4) = 8
Third Evaluation : ( i = 3) // i = 3
lastly : 8 * 3 = 24, which is not the correct answer.

Therefore my question is that, why the expression
inside the brackets are not evaluated first??
Thanks Ajith,
Looking forward for your reply



 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi karthik,
I feel u should look it in this way.
all operators like +/*.... except '=' are left associative.so first of all the LHS operand is evaluated then RHS operand is evaluated and only after that the operation is performed.
hope this is of some help
regards
deekasha
 
Karthik Subramanian
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Deekasha,
I fully agree with your point that all operators, excluding = ,op=,Unary operators
operate from left to right but then doesn't the brackets i,e () sit on top of the operators precedence hierarchy.
Meaning to say that whatever operators are
present in an expression, the expression which is included inside the inner most
paranthesis is to be evaluated first.
Correct me if i am wrong.
Sorry to be so nagging!! but any replies??
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic