This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

tricky one i guess!!!!!!!!!!!! need help  RSS feed

 
Ganesh kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
though most of u would have tried this.......can any of u give me a proper explanation to this question.....


int i=10;
i = i++;
sop(i);

y doesn't the value of i in the sop change to 11 ???
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search the forum. This has been asked and explained many times.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the syntax to increment i is "i++", not "i = i++".
 
Greeshma Swaminathan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because you are using the post increment operator.
ie, when u give j=i++, first the original value will be assigned to j and then later i gets incremented.In ur case, u are doing i=i++.
The assignment takes place as below.
int temp=10;
i++ ie, i=11
i=temp.
so i gets the value 10 itself. (The above steps r just for expln.)

But if u use a pre increment operator, this problem will b solved, bcoz
i=++i means it will increment first and then only it will assign the value.
Give it a try and do read more about the differnce of these operators.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should take a look at both http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment and http://faq.javaranch.com/view?UseAMeaningfulSubjectLine
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Ganesh",
please see the requests for you to change your display name here and here.

Yo may feel you're being picked on, but we believe these points are important to maintain the quality of our site. This will be the last request to change your display name before your account is closed, please change it immediately, it will be worth it

thanks,
Dave.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!