| Author |
doubt related to "++" operator
|
Sowjanya Chowdary
Ranch Hand
Joined: Aug 22, 2005
Posts: 35
|
|
Hi everybody, i had just started preparing for scjp1.4 Please clear my doubt in the following code: class abc { public static void main (String[] args) { int i=10; i=i++; //(1) System.out.println(i); } } The program is printing 10. Why is it so, eventhough 'i' is getting post incremented at (1). Thanks for the help.
|
 |
madhup narain
Ranch Hand
Joined: Dec 14, 2004
Posts: 148
|
|
|
good question.... im wondering... and looking for a good answer..
|
Money for nothing and Java for Free
SCJP, SCWCD
|
 |
Patrick van Zandbeek
Ranch Hand
Joined: Aug 17, 2005
Posts: 37
|
|
good question apparantly the ++ gets lost when you assign it to the same variable. If you make an extra variable, j for instance and then say j=i++; j would be 10 and i would be 11 when printing after that. Apparantly in i=i++; the ++ gets skipped after assigning i to i. Having said that, i=i++ isn't what you want to be coding. If you need to increment i just say i++; not i=i++; or possibly i=i+1; if you can't live without an = sign. Perhaps someone can explain more technically what happens during i+i++; but as far as I'm concerned, the bottom line is that you shouldn't write your code like that anyway.
|
u is a letter, not a word.
|
 |
David Ulicny
Ranch Hand
Joined: Aug 04, 2004
Posts: 724
|
|
use just i++; or i=++i;
|
SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
|
 |
Sowjanya Chowdary
Ranch Hand
Joined: Aug 22, 2005
Posts: 35
|
|
This was my first question to java ranch. Happy to see prompt replies. Even though i know it is better to write i++ rather than i=i++ , i just wanted to know the reason of strange behavior. Thanks once again.
|
 |
Arun Kumarr
Ranch Hand
Joined: May 16, 2005
Posts: 508
|
|
LHS= left hand side. RHS= right hand side. when you say, i=10; i = i++; i(RHS) is incremented to 11 and the old value (10) is remembered and set to i(LHS); when you say, i=10; i=++i; i(RHS) is incremented to 11 and then i(LHS) is set to 11.
|
If you are not laughing at yourself, then you just didn't get the joke.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Originally posted by Amrutha Ch: This was my first question to java ranch. Happy to see prompt replies.
As you are new to Javaranch, you should read this. A quick search would have revealed this question has been asked many times, even in the short time I've been coming here.
|
Joanne
|
 |
Niyas Ahmed Sheikh
Ranch Hand
Joined: Jun 15, 2005
Posts: 129
|
|
I have some doubts regd this post:
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by Niyas Ahmed Sheikh: I have some doubts regd this post:
What do you think it *should* print, and why?
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Niyas Ahmed Sheikh
Ranch Hand
Joined: Jun 15, 2005
Posts: 129
|
|
|
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9946
|
|
you forget that the pre-increment operator acts first. so, the first thing that happens is i is incremented to 11. then, you add i + i, to get 22. i is then post incremented to 12. 22 is then assigned to i. print 22.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
 |
|
|
subject: doubt related to "++" operator
|
|
|