permaculture playing cards*
The moose likes Beginning Java and the fly likes Incrementing by 1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Incrementing by 1" Watch "Incrementing by 1" New topic
Author

Incrementing by 1

Jessica Lang
Ranch Hand

Joined: Jul 23, 2002
Posts: 61
Hello,

In the above program, statement (sum=sum+1; ) will generate an output of 18. However, if (sum=sum++; ) is used, the output is 0. What is the difference of both statements in this case? Isn�t both statements are performing the same function, ie., incrementing the sum variable by 1 and add it to the current sum value? Why is the later statement (sum=sum++; ) generating a 0 value?
Thanks....
Code tags added, winkies removed - BFG
[ May 15, 2003: Message edited by: Barry Gaunt ]
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Jessica
I think that a few discussion about this case before.
As we know that the effect taken will be after the syntax ';'
Now you notice that the same variable name and the effect must be cancel by the assignment to sum.
Because the action firstly is assignment then the increment. Assignment to the same variable name that will cancel the effect of increment
You can imagine actually sum++ assign to be sum then sum has not postfix increment;
So, the result without changed
Hope this help

[ May 15, 2003: Message edited by: siu chung man ]

Francis Siu
SCJP, MCDBA
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Instead of sum = sum++; you should use just sum++; on its own.
You can find out the reason why by searching the SCJP discussion forum, where the topic has been discussed many times ( I'm feeling lazy today )
-Barry
[ May 15, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jessica Lang
Ranch Hand

Joined: Jul 23, 2002
Posts: 61
Hi Siu,
You mentioned this "Assignment to the same variable name that will cancel the effect of increment" earlier on. Is this what is 'causing the sum value to be 0?
I am still puzzled how the zero value is derived. I understand the meaning of post and pre increments. Even so, I would expect the value to be greater than 0.....
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This may help.

Updated URL of JLS 15.14.1
[ May 16, 2003: Message edited by: Barry Gaunt ]
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
Firstly,you read the link given by Barry
Assignment to the same variable name that will cancel the effect of increment" earlier on. Is this what is 'causing the sum value to be 0?
Yes,postfix increment only,you can try the following

You may find out some interesting and know more increment effect.
Jessica Lang
Ranch Hand

Joined: Jul 23, 2002
Posts: 61
Hello,
**************************************************
*** IncrementSum.java
public class IncrementSum {
public static void main (String args[]) {
int SumOne=0;
int SumTwo=0;
int SumThree=0;
int SumFour=0;
int SumFive=0;
int SumSix=0;
int SumSeven=0;
SumOne=SumOne++;
SumTwo=++SumTwo;
SumThree=SumThree+1;
SumFour++;
++SumFive;
SumSix=(SumSix++)+SumSix;
SumSeven=(SumSeven++)+(SumSeven++);
System.out.println("SumOne=SumOne++ is " + SumOne);
System.out.println("SumTwo=++SumTwo is " + SumTwo);
System.out.println("SumThree=SumThree+1 is " + SumThree);
System.out.println("SumFour++ is " + SumFour);
System.out.println("++SumFive is " + SumFive);
System.out.println("SumSix=(SumSix++)+SumSix is " + SumSix);
System.out.println("SumSeven=(SumSeven++)+(SumSeven++) is " + SumSeven);
}
}
**************************************************
*** Output:
SumOne=SumOne++ is 0
SumTwo=++SumTwo is 1
SumThree=SumThree+1 is 1
SumFour++ is 1
++SumFive is 1
SumSix=(SumSix++)+SumSix is 1
SumSeven=(SumSeven++)+(SumSeven++) is 1
**************************************************
I ran a few tests as above and come to a conclusion that "in assignment of the same variable name will cancel a postfix incremental value/effect". At first, I expected "SumOne=SumOne++" to have the same effect as "SumFour++", ie. a value of 1. Somehow, this is how Java behaves, which I do not have a logical explanation, but just need to ACCEPT it.... :roll:
Looking at SumSix and SumSeven, is there a logical explanation for it?

Thanks for the helps....:-)
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
At first, I expected "SumOne=SumOne++" to have the same effect as "SumFour++", ie. a value of 1. Somehow, this is how Java behaves, which I do not have a logical explanation, but just need to ACCEPT it....
C and C++ behave the same way. All you have to understand is that postfix operators return the value before incrementing and prefix operators return the value afterincrementing. So:

In the above code we initialize someInt to 1. In the second statement someInt is incremented as expected, but the return value is then assigned back to itself, so there is apparently no change in someInt because postfix operators return their value before incrementing.
Now:

In the above code since the prefix operator returns its value after incrementing, then now someInt will show the change. Of course all that was necessary to get the same effect would be to just do ++someInt.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Jessica
Let me explain more according to Michael
At first, I expected "SumOne=SumOne++" to have the same effect as "SumFour++", ie. a value of 1. Somehow, this is how Java behaves, which I do not have a logical explanation, but just need to ACCEPT it.... :roll:
Change a little bit of your coding,you may begin to believe Java that has the logical explanation.

Do you see the different of the logical explanation?
Does this help?
Jessica Lang
Ranch Hand

Joined: Jul 23, 2002
Posts: 61
After much explanations and program tries, I begin to see the pattern of the (SumOne=SumOne++) that SumOne will overrides the postfix increment of SumOne, thus resulted in value of 0.
Thanks for all your helps!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Incrementing by 1
 
Similar Threads
Doubt In Kathy& Bates book questions at page 198
explain this ...please
Fairly new to java wondering if I could get some help.
Need explanations of inheritance
Please Help