This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes doubt related to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt related to "++" operator" Watch "doubt related to "++" operator" New topic

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)

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
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 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

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: 661

LHS= left hand side.
RHS= right hand side.

when you say,
i = i++;

i(RHS) is incremented to 11 and the old value (10) is remembered and set to i(LHS);

when you say,


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

Joined: Aug 05, 2005
Posts: 3742
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.

Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
I have some doubts regd this post:

Ilja Preuss

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

Joined: Oct 02, 2003
Posts: 11957

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: doubt related to "++" operator
It's not a secret anymore!