This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes increment operator '++' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "increment operator Watch "increment operator New topic
Author

increment operator '++'

s khosa
Ranch Hand

Joined: May 15, 2003
Posts: 72
In following piece of code why is 'i' still 0 after i++?
int i=0;
i=i++;
System.out.println(i);
In expression 'i=i++' first value 0 is assigned to i and then i++ is done. Would ++ operator increment value of i by '1', which is '0' after assignment? So i should be 1?
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
If the line was simply i++; then i would be 1.
The trick here is the line in your code (i=i++ actually gives the variable i the value of i before the incrementation, thus the ++ has no effect on the newly assigned variable i.
Dave Johnson
Ranch Hand

Joined: May 25, 2003
Posts: 111
Sorry about the smilie, total mistake caused by ;) & me not disabling the bloody thing.
La Vish
Ranch Hand

Joined: Apr 17, 2002
Posts: 158
Hi Dave,
You can actually use the edit button to disable the smiley face so that you don't have to feel sorry for that!


La Vish
SCJP 1.4, President 60s Club
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi sumeer
You can find the related threads click here you will find a interesting nested link too.....


Francis Siu
SCJP, MCDBA
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: increment operator '++'
 
Similar Threads
Mock Exam doubt increment expresion
Output of this...........?How you got that?
confusion .....
Doubt with Kathy sierra chapter 10
Incremeant - Marcus Greens Exam-3, Q-54