Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt related to "++" operator

 
Sowjanya Chowdary
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good question.... im wondering... and looking for a good answer..
 
Patrick van Zandbeek
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use just
i++;
or
i=++i;
 
Sowjanya Chowdary
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some doubts regd this post:







 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Niyas Ahmed Sheikh:
I have some doubts regd this post:


What do you think it *should* print, and why?
 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic