aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes NO.54 from Marcus green - Exam3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NO.54 from Marcus green - Exam3" Watch "NO.54 from Marcus green - Exam3" New topic
Author

NO.54 from Marcus green - Exam3

guo mark
Ranch Hand

Joined: Oct 20, 2001
Posts: 43
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++; //confused
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

quite confused me,in my opinion ,in line confused
no matter 'i' will get the value of 0 first or '++' first it will get value 1 at last.There are someone kind to explain it completely?3x


meet once ,love every while
Chitra Jay
Ranch Hand

Joined: May 02, 2002
Posts: 76
Hi guo,
This is my understanding.
The above program when run prints the output as 0.
When inc.fermin(i) is called,a copy of the 'i' gets passed and it gets incremented inside the fermin(),which is not visible to the caller.so the 'i' value of 0 remains the same inside main.
i=i++ assigns the value of 0 to i ,since its postincrement.Therefore,the output is 0.
Nazmul Huda Sarkar
Ranch Hand

Joined: Feb 01, 2002
Posts: 317
This is also confusing to me. You see
1. i=1;
2. i=i++;
3. System.out.println(i);
In line 2 though it is post increement, in line 3 it supposed to be incremented. But still it is 1.
But here
1. i=1, j=1;
2. j=i++;
3. System.out.println(i);
in line 3 it prints 2.
Anyone....?


Nazmul<br />SCJP,SCWCD,IBM OOAD with UML
Selvi Ranjan
Greenhorn

Joined: Mar 16, 2002
Posts: 9
The way I understand is
i = i++;
There are two operator in the above statement = and ++
++ has higher precedence so execute first the value of i is returned and then incremented
= operator has the return value 0 and it assign it to i. So the value that been incremented by ++
will be overwritten by =
If I am wrong please correct me.
Selvi Ranjan
guo mark
Ranch Hand

Joined: Oct 20, 2001
Posts: 43
not clear,some one would go on ?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi All,
Let me clarify things a liitle more(Hope I'm able to do it )
1 i=0;
2 inc.fermin(i);
3 i = i++;
Line number 2 has no affect on the value of i , since i is passed as value and not by reference. So after line no. 2 value of i still remains 0.
Now , coming to line number 3. Post increment operator "++" has higher precedence than "=" , so JVM will first execute i++ on RHS. But according to nature of the post operator , it's initial(i.e zero) value is assigned first and then it's value is incremented. So i on LHS is assigned a value of '0' i.e i=0.
After this step one doubt comes in the mind that increment to i still remains. This increment is supported in languages like 'C' or 'C++' but not in Java . That is in case of 'C' or 'C++' the answer would have been 1 but not zero(as in case of Java)
i hope this further clarifies the situation.
Regards,
Harneet
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Okay, let's look at the expression:

In order to evaluate this expression, the JVM first evaluates each of the operands. Therefore, the first step is to get the value of i++.
As we're using a post-increment, the value used for i is the initial value and it is then incremented. Therefore, the value 0 will be used to evaluate this expression, but now i has the value 1 stored in it.
However, the JVM will now evaluate this expression:

When this occurs, the value that i contained, 1, is overwritten by the 0. Therefore performing an operation such as:

will always result in i having the same value it had to begin with. However, had you used ++i, we would obtain a successful increment.
I hope that helps,
Corey


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NO.54 from Marcus green - Exam3