aspose file tools*
The moose likes Beginning Java and the fly likes operator ++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "operator ++" Watch "operator ++" New topic
Author

operator ++

sahil kumar garg sangrur
Greenhorn

Joined: Jun 05, 2012
Posts: 16
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

Joined: May 30, 2012
Posts: 679
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
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7639
    
  19

sahil kumar garg sangrur wrote:why the output is 0 not 1

What do you think it should be? And WHY?

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

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.

Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
sahil kumar garg sangrur
Greenhorn

Joined: Jun 05, 2012
Posts: 16
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

Joined: May 12, 2013
Posts: 47
    
    1
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

Joined: Jun 09, 2013
Posts: 3
How does the link posted by Henry Wong not give you the exact reason?
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operator ++