Forums Register Login

Did not get this either!!

+Pie Number of slices to send: Send

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
+Pie Number of slices to send: Send
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).]
+Pie Number of slices to send: Send

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



+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send

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
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 790 times.
Similar Threads
ques
assertions
Static initialization
label continue
post increment operator
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:57:53.