This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NO.54 from Marcus green - Exam3

 
guo mark
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Chitra Jay
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....?
 
Selvi Ranjan
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not clear,some one would go on ?
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic