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

operator ++

 
sahil kumar garg sangrur
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i=0;
i=i++;
System.out.println(i);
// this will print 0 not 1
please can anybody explain me how this will work
why the output is 0 not 1
thanks in advance
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sahil kumar garg sangrur wrote:int i=0;
i=i++;
System.out.println(i);
// this will print 0 not 1
please can anybody explain me how this will work
why the output is 0 not 1
thanks in advance

You're not the first to ask this question (you're probably not even in the top 100). Search these forums and you will find plenty of explanations.
 
Henry Wong
author
Marshal
Pie
Posts: 21513
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:
You're not the first to ask this question (you're probably not even in the top 100). Search these forums and you will find plenty of explanations.


It is also mentioned in the FAQs...

https://www.coderanch.com/how-to/java/PostIncrementOperatorAndAssignment

Henry
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sahil kumar garg sangrur wrote:why the output is 0 not 1

What do you think it should be? And WHY?

Winston
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Funny and an irony, people ask these questions without once showing any sort of an effort from their side.
@OP, the world wide web is a plethora of resources, even an iota of your thought process on why this was the result could have sufficed. Asking for clarification on such open ended questions to which there is a mine of answers both in print and the web only invokes ire from people who painstakingly read, devise premises followed by conclusions. And if those conclusions do not match the output or the result, they seek help here.
 
sahil kumar garg sangrur
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am not getting the exact reason for this so please give the exact reason of this if any body knows it thanks for those guys who reply
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From my very little experience:

int i=0;

If you want to increment i by 1 then you either do this:

i = i + 1;

or this:

i++;

But this is wrong: i = i++;
 
Jasper Verelst
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the link posted by Henry Wong not give you the exact reason?
 
Ivan Jozsef Balazs
Rancher
Posts: 982
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles D. Ward wrote:
But this is wrong: i = i++;


Well, not good style and better avoided indeed. But (in contrary co C) the outcome is at least defined in Java.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic