Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Harish Kashyap
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's some discussion of just this sort of problem in anther thread here. Check it out.
 
Don Smathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would only get an answer of 2 if you changed the statement to:
int i=1;
i= ++i;
 
usman ahmad
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
++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
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about "i += 1", or is that too unpretty?
 
usman ahmad
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic