File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes i=i++ .................................? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "i=i++ .................................?" Watch "i=i++ .................................?" New topic
Author

i=i++ .................................?

Harish Kashyap
Ranch Hand

Joined: Jun 14, 2000
Posts: 118
class abc
{
public static void main(String[] ab)
{
int i=1;
i=i++;
System.out.print("i="+i);
}
gives i=1
why???
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
There's some discussion of just this sort of problem in anther thread here. Check it out.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Don Smathers
Ranch Hand

Joined: Mar 04, 2001
Posts: 31
Your are not incrementing i until AFTER the assignment to itself.
Thus the value of i that is displayed remains unchanged.
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
You would only get an answer of 2 if you changed the statement to:
int i=1;
i= ++i;
usman ahmad
Ranch Hand

Joined: Mar 06, 2001
Posts: 46
++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 Goss
Ranch Hand

Joined: Mar 21, 2001
Posts: 169
what about "i += 1", or is that too unpretty?
usman ahmad
Ranch Hand

Joined: Mar 06, 2001
Posts: 46
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
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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
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).]
Stuart Goss
Ranch Hand

Joined: Mar 21, 2001
Posts: 169
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
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
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
 
 
subject: i=i++ .................................?