++i and i++ ; The first one is called the prefix increment operator and the second one is called the postfix increment operator. Both operators perform the function of incrementing. Howerver, these operators function differently. When you use the prefix(++i); the result is calculated and stored, and then the variable is used.For example: a=4; b=++a; results in both a and b hold the same value 5. When you use postfix (i++), the variable is used and then the result is calculatedand stored. For example if a=4; b=a++; 4 will be assigned to b, and then after assignment a will be incremented. In Your case: int i=1; i=i++; It is example of Postfix increment operator. So here "i"(i++) assigns the value 1 to i.And we print this value of i in our println statement.That is why it is printing one.If you read the example of postfix increment operator carfully you will get the point. I hope this is helpful Regards Usman
Stuart! That thing works! But the problem here is even after writing many post increment statements the result remains the same. That's what has to be figured out. Regards Usman
You don't get it yet??? OK one more time. variable i=1 the working value of i is 1 set variable i= working value of i therefore it is 1. increment the working value of i to 2 now we are done and print the value in the variable i which is 1. if we were to continue and do something more with i like: j = i; set variable j = working value of i - then j would be 2.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Sorry Cindy, I think this thread has gone a few replies to many. Your explanation is misleading and wrong. Operator precedence says ++ happens before assignment regardless of pre or post. If we continue to use the value of i we will not get an incremented value but the original value. The following code will prove the point.
The steps that happen: 1. current value of i is stored in memory (save 1) 2. current value of i is incremented (i = 2) 3. stored memory value is assigned to i (i = 1) 4. life goes on ... Regards, Manfred.
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
Absolutely correct. What I meant to use for the example was j=i++; i=i; System.out.println("i = " + i); But I shouldn't have rushed through the answer. Thanks for correcting me. [This message has been edited by Cindy Glass (edited March 23, 2001).]
Did I understand properly? Would it be better in the first example, simply to write "i++;" or even "++i;" on its own w/o equals sign? :P Stuart.
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
The point is that the assignment back to i can be confusing. Of course you don't EVER have to use post-increment operators (you can see that they can cause confusion). However when we DO run into them, we need to understand them even when they are mixed with assignments.
Don Smathers
Ranch Hand
Joined: Mar 04, 2001
Posts: 31
posted
0
Harish -- if you intent was to output 2, then why not use System.out.println( ++i ); // i was 1, now it's 2 - works fine if you simply wanted to know what was happening in your inquiry, this is well covered above !! Don