File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Incrementing by 1

 
Jessica Lang
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 867
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jessica Lang
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may help.

Updated URL of JLS 15.14.1
[ May 16, 2003: Message edited by: Barry Gaunt ]
 
Francis Siu
Ranch Hand
Posts: 867
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Francis Siu
Ranch Hand
Posts: 867
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic