This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Did not get this either!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Did not get this either!!" Watch "Did not get this either!!" New topic

Did not get this either!!

Karthik Subramanian

Joined: Aug 23, 2000
Posts: 27

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);//
int y = z() * (z() + z());
private static int z() {
return ++x;
Answer 9:
The output is:

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

Joined: Mar 17, 2000
Posts: 5782
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 -


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
[This message has been edited by Ajith Kallambella (edited August 31, 2000).]

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Karthik Subramanian

Joined: Aug 23, 2000
Posts: 27

Dear Ajith,
I am really thankful to you for pondering over this
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

Joined: May 06, 2000
Posts: 396
Hi karthik,
I feel u should look it in this way.
all operators like +/*.... except '=' are left 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
Karthik Subramanian

Joined: Aug 23, 2000
Posts: 27

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??
I agree. Here's the link:
subject: Did not get this either!!
jQuery in Action, 3rd edition