This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Marcus Exam 3 Qn# 54

Suresh
Ranch Hand
Posts: 76
Question 54)
What will happen when you attempt to compile and run the following code?
public class Inc{
public static void main(String argv[]){
Inc inc = new Inc();
int i =0;
inc.fermin(i);
i = i++;
System.out.println(i);
}
void fermin(int i){
i++;
}
}
1) Compile time error
2) Output of 2
3) Output of 1
4) Output of 0

In the above code, I thought the answer was option 3 (value 1). But the answer given was option 4 (value 0). I compiled the program and the answer is 0.
But, in i = i++;
i is assigned to 0 and then i is incremented by 1. So, at the end of the expression, i should be 1. But, how is it evaluating to 0?
The explanation for the answer is given as

The method fermin only receives a copy of the variable i and any modifications to it are not reflected in the version in the calling method. The post increment operator ++ effectivly modifes the value of i after the initial value has been assiged to the left hand side of the equals operator. This can be a very tricky conept to understand

I don't really understand the tricky concept here!! How is it evaluating to 0?
Regards,
Suresh.

Ajay Kumar
Ranch Hand
Posts: 87
Hi Suresh,
Think of the statement
i = i++;
as follows:
int temp = i++;
i = temp;

Since the LHS of the expression is assigned before the increment the value is still 0.
If U assign the values of the i before and after the inc. as follows:
i = 0 (1); --> the bracketed value is after the increment
the value os i is still 0.
As Marcus had pointed out, U need to be a lot careful in these type of sits.
HTH

------------------
Regds
Ajay Kumar

Deepa sivasankar
Ranch Hand
Posts: 35
Suresh,
i = i++;
System.out.println(i);
How can you say i in the println is the one before post increment. i = 1 that should be the answer.

Indy
Ranch Hand
Posts: 44
I don't get it either:
i=i++, undoubtedly, i is assigned first to 0 before the increment, however, after the assignment, i should be incresed to 1 while the ++ is execused. I can't compile it right now, if Suresh has the code compiled and run already, then, the compiler should be correct. anyone clear it please?
by the way, Suresh, could you tell me the site of the complete version of Marcus's 3? mine only contain 39 questions. Thanks in advance.
Indy

Suresh
Ranch Hand
Posts: 76
But Ajay,
i = 0 (1) is acceptable. But the i should be incremented after assigning 0 to i. Right? Then, it should become 1.
Can you make it bit more clear? Sorry, if I am asking the basics!!!
No Deepa,
I compiled the program, and I got only 0. Pls. refer to Ajay's explanation before.

Indy,
Marcus's exam3 is at
http://www.jchq.net/mockexams/exam3.htm .
Regards,
Suresh.
[This message has been edited by Suresh (edited May 19, 2000).]
[This message has been edited by Suresh (edited May 19, 2000).]

Ajay Kumar
Ranch Hand
Posts: 87
Suresh,
i = 0 (1) is acceptable. But the i should be incremented after assigning 0 to i. Right? Then, it should become 1.
Can you make it bit more clear? Sorry, if I am asking the basics!!!

I know these ++ and -- can be confusing.
In this case the value outside the bracket is assigned to i.
<pre>
public class Clar {
public static void main (String [] args) {
int i = 0;
i = i++;
System.out.println(" i = "+ i);
int j = i++;
System.out.println(" i = "+ i+" j = "+j);
}
}
</pre>
Remember the value is assigned first and then incremented. So the assigned value in your example is still 0.
Test this code, U will find interesting results. Try to reason out the results. I am always glad to explain them.

------------------
Regds
Ajay Kumar

[This message has been edited by Ajay Kumar (edited May 19, 2000).]

Indy
Ranch Hand
Posts: 44
this "1++" is still bugging me.
Ajay. I compiled and run your code. as expected, the output is:
i = 0
i = 1, j = 0
the second line makes prefect sense, the assingment happens first, j was assigned to 0. then i incremented from 0 to 1.
however, to the first line, i to the left hand was assigned to 0.
Indy

Remco Rotteveel
Greenhorn
Posts: 8
As I understand, an expression is first evaluated from left to right, and then assigned from right to left. So in the case of i = i++;, this is what happens:
1) evaluation:
LHS: unchanged
RHS: i is incremented (to 1), but previous value (0) is kept for assignment
2) assignment:
i = 0;
This problem has a lot to do with the fact that the assignment operator (=) has the lowest precedence of all, so it is executed last. A similar problem is this:
int[] x = new int[5];
int a = 0;
x[a] = a = 3;
which results in x[0] = 3, and a = 3.
So i++ doesn't mean "use the current value of i and increment it when everything else is done," but rather "increment i, but use the previous value for any assignments in the same expression."
Regards,
Remco.

[This message has been edited by Remco Rotteveel (edited May 20, 2000).]

Ajay Kumar
Ranch Hand
Posts: 87
Indy,
Do not take the two statements separately. Take them as a combined one.
Ajay. I compiled and run your code. as expected, the output is:
i = 0
i = 1, j = 0
the second line makes prefect sense, the assingment happens first, j was assigned to 0. then i incremented from 0 to 1.
however, to the first line, i to the left hand was assigned to 0.

i = i++
here the value of i was 0 before the expression is executed.
so the valuee of i is assigned as 0 abnd then it is incremented. But there is no place to store the value 1 after the increment since assignment has already been made. So it is lost.
This is where the second line comes into play.
int j = i++
Note I am not initialising the value of i before this statement. I am using the same value from the previous expression.
Here also the value of i (still 0) is assigned to j(hence j = 0) and then the value of i is incremented. Since the variable i has still not been assigned to, it takes up the new value of 1.
Hope this clears up some of the doubts. I would be glad if somebody more eloquent can put it better. I was never a born teacher U see.
------------------
Regds
Ajay Kumar

Suresh
Ranch Hand
Posts: 76
Ajay,
That makes sense, I believe!!
So, once i is assigned the value, the increment is left!!
Thanks a lot Ajay and Remco!!
Regards,
Suresh.

Marcus Green
arch rival
Rancher
Posts: 2813
This is a deeply horrible confusing question and if I got it on the exam I probably wouldn't get the right answer. And I wrote it!
Marcus

Edward Man
Ranch Hand
Posts: 40
This is a disassembled listing of part of the program.
Method void main(java.lang.String[])
0 new #1 < Class Inc >
3 dup
4 invokespecial #5 < Method Inc() >
7 astore_1
8 iconst_0 // load a constant 0 to stack
9 istore_2 // store the constant 0 to variable 2, it is "i=0"
11 iload_2 // push variable 2 to stack (0 is now in stack)
12 invokevirtual #7 < Method void fermin(int) >
15 iload_2 // push variable 2 to stack again (value = 0)
16 iinc 2 1 // increment the variable 2 from 0 to 1
19 istore_2 // store stack content to variable 2, overwrite
20 getstatic #8 < Field java.io.PrintStream out >
24 invokevirtual #9 < Method void println(int) >
27 return
Location 8 and 9 are basically equal to "int i = 0;".
Location 11 puts a copy of i as an argument of fermin. So whatever fermin does, it won't affect "int i", because the result is not going to store back to "int i".
Location 15-19 is effectively "i = i++;".
Location 15 puts a copy of i onto the stack (Puts a 0 onto stack because i contains 0).
Location 16 then increments the variable 2(it is "int i"), so the content of variable 2 becomes 1. The value in stack is still 0 (the value before variable 2 is incremented).
Location 19 then stores whatever in stack back to variable 2 (back to "int i"). It now stores back 0 to variable 2 ("int i"). So i is still 0.
I hope this low level explanation helps.
Edward

[This message has been edited by Edward Man (edited May 21, 2000).]

Suresh
Ranch Hand
Posts: 76
Edward man,
That was a very nice explanation!! Still I don't know how to disassemble the code!! Can you tell me how to do it, sometime when you are free?
Marcus,
Thanks for getting in!! But, I don't think these kind of things can be manually worked out!!
Regards,
Suresh.

Anonymous
Ranch Hand
Posts: 18944
Suresh,
to dissassemble your code, use the following command on the prompt
javap - ClassName > file.ext
This file will have the dissassembled code that Edward talks about

Greg Whelan
Ranch Hand
Posts: 52
As an additional complication, try to predict what this prints:

The first one should be easy

Edward Man
Ranch Hand
Posts: 40
The command looks more like:
javap -c Inc > Inc.txt
javap -help
will give you all the options.
Lionel, I hope you don't mind I make a slight modification to your command.
Edward

Suresh
Ranch Hand
Posts: 76
Greg,
I am really in chaos I couldn't predict anything, except the first one (thanks to previous replies ). But other outputs, I don't think I could have predicted!! The output I got was
0
1
3
6
Now, I am more than the previous question!!
I am taking my exam coming Saturday!! Can somebody tell me for sure that these kinds of questions won't be in the exam? Otherwise, my confidence will be shattered!!
Regards,
Suresh.

Remco Rotteveel
Greenhorn
Posts: 8
If you follow the rules in my previous posting, everything works out. I'll walk thru k = k++ + k++ + k++ as an example:
Rule #1: Expressions are evaluated from left to right (and assigned from right to left, but that is irrelevant if there is only one '=' sign).
Rule #2: k++ means "increment k (immediately), but use the old value in the current expression".
Rule #3: Assignments have lowest precedence, so they are performed last.
So if we follow these rules, this is what happens with k = k++ + k++ + k++:
before: k = 0
after first k++: k = 1, but a 0 is used in the expression (k = 0 + k++ + k++)
after second k++: k = 2, but a 1 is used in the expression (k = 0 + 1 + k++)
after third k++: k = 3, but a 2 is used in the expression (k = 0 + 1 + 2)
after assignment: k = 3 (0 + 1 + 2)
Hope this helps,
Remco.

Anonymous
Ranch Hand
Posts: 18944
To answer this quetion first compute the values of post increment. Remember that the post increment returns the value first and then increments
So,
int i = 0;
//here the value assigned to i is 0, (incrementing is ignored as you assigned the variable to the value returned by the post-increment
i = i++;
System.out.println(i);
int j = 0;
//first j is 0 then 1. Now add them all and assign it to j
j = j++ + j++;
System.out.println(j);
int k = 0;
k = k++ + k++ + k++;
//first k is 0, then 1, then 2 . Add them and assign it to j
System.out.println(k);
int l = 0;
//first l is 0, then 1, then 2, then 3 . Add them and assign it to j
l = l++ + l++ + l++ + l++;
System.out.println(l);

Hence the values are 0, 1, 3, 6
Think this should help
[This message has been edited by Lionel (edited May 22, 2000).]

Suresh
Ranch Hand
Posts: 76
Thank you Lionel, Remco and Edward!!
That was a great concept to me, highly simplified by you people!! Thanks a lot!!
Regards,
Suresh.

Indy
Ranch Hand
Posts: 44
you guys've done a terrific job. I have all my doubtness cleared.
Thank you very very very much!
Marcus, that really lightern up my mood.
Indy