• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Post Increment Operator

 
Savio Mascarenhas
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen when you attempt to compile and run the following code?
1.public class Inc
2.{
3. public static void main(String argv[])
4. {
5. Inc inc = new Inc();
6. int i =0;
7. inc.fermin(i);
8. i = i++;
9. System.out.println(i);
10. }
11. void fermin(int i){
12 i++;
13. }
14.}
1) Compile time error
2) Output of 2
3) Output of 1
4) Output of 0
The result is (4).
In line 8,the value of 'i'(before increment ie. '0')is assigned .After the assignment ,a post increment operation takes place(on the "same variable"),which increase the value of 'i'.
Why is the new value of 'i' not reflected when printed ?
[This message has been edited by Savio Mascarenhas (edited December 11, 2000).]
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Savio,
Operand values are evaluated from left to right. And the operation is evaluated from right to left.
i++ is the same as i = i + 1
hence we can break this down into two expressions
(i = i) -> last expression from the right
(i = i + 1) -> first expression from the right.
The operand values are evaluated from left to right and stored.
So when the last expression executes the stored value of i which is 0 is assigned to i.

Rgds
Sahir
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i have seen this too many times. I am going to try to repeat someones assembly language explanation. forgive me.
register A holds i
register B holds i
post increment means assign first
A=B
now increment B
now what is A?
B gets thrown out the window
end of story.
 
Amit Goel
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i agree with above two answers but in your case the increment taking place in the method is taking place for the object created for inc class.
and it has its own value of i other than the i mentioned in main method .
if you have read about the concept of objects then you can understand
also , in a post increment operator , the value assigned is used first and then incremented.
i hope you get my point.
 
Savio Mascarenhas
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Randall Twede:
ok i have seen this too many times. I am going to try to repeat someones assembly language explanation. forgive me.
register A holds i
register B holds i
post increment means assign first
A=B
now increment B
now what is A?
B gets thrown out the window
end of story.

Randall,
Are you trying to say that the post incremented variable 'i' is created at another memory location even though there exists a variable 'i'ie.the one declared earlier at line 6 and that both these variables do not have anything to do with each other even though they have the "same name" ?
Pls let me know if i'm wrong .
[This message has been edited by Savio Mascarenhas (edited December 11, 2000).]
 
Jini Varghese
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the same doubt as Savio does. Are there tow "i"s?. In the statement
i= i++;
The value of i (0) is assigned to LHS i. But, after the assigning, RHS i will get incremented to 1. So, the value printed must be 1, right?.
I did not understand the 'magic' !!
Forgive me.
 
Abhijit Parwatkar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I think...
i = 0
i = i++
Step by step now..
1. i = i++
2. Two operations i = i , i = i + 1
3. Substitute value of i in each i = 0 , i = 0 + 1
4. evaluate each i = 0 , i = 1
5. Right to left so set i = 1 and then set i = 0
6. Thus i = 0

[This message has been edited by Abhijit Parwatkar (edited December 11, 2000).]
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are two things being shown in the example. you might think after line 7 i=1 but primatives are passed by value to methods(unlike objects) so i remains unchanged outside the method. I,m sorry if my explanation of i=i++ was confusing. what i meant was the processor goes to memory and gets i. it puts it in two registers inside so it can perform the operation then it sends it back to memory. if it was j=i++ it would send them both back to memory when done. perhaps it was not a good explanation.
 
S Sivasuthan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Abhijit Parwatkar
Good Explanation Indeed.
-Siva

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic