File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Post Increment Operator 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 "Post Increment Operator" Watch "Post Increment Operator" New topic
Author

Post Increment Operator

Savio Mascarenhas
Ranch Hand

Joined: Nov 29, 2000
Posts: 108
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

Joined: Nov 05, 2000
Posts: 158

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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.


SCJP
Visit my download page
Amit Goel
Ranch Hand

Joined: Dec 07, 2000
Posts: 50
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.


Amit<br /> <br />The Less I have, The more I gain..Off the Beaten Path, I Reign.
Savio Mascarenhas
Ranch Hand

Joined: Nov 29, 2000
Posts: 108
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

Joined: Dec 06, 2000
Posts: 58
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

Joined: Dec 07, 2000
Posts: 10
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Nov 29, 2000
Posts: 6
Thanks Abhijit Parwatkar
Good Explanation Indeed.
-Siva

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Post Increment Operator