Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

doubt related to "++" operator

 
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.
 
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..
 
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.
 
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.
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • 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.
 
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.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some doubts regd this post:







 
author
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
 
lowercase baba
Posts: 12893
63
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic