Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# ++ confusion

gaurav gupta sitm
Greenhorn

Joined: Jan 12, 2011
Posts: 22

Output : 4
a value must be increased from 4 to 5 , but why it is not increased ???
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3094

2
Because a++ returns the old value, which is 2, and the a on the left is evaluated before the a++ is performed, so all it does is a = 2 + 2.
mahesh chandra
Greenhorn

Joined: Dec 18, 2007
Posts: 12
Because a++ returns the old value, which is 2, and the a on the left is evaluated before the a++ is performed, so all it does is a = 2 + 2

@Stephan
I agree that a++ returns old value. statement will be equal to 2+2. What about post increment of variable a. value of variable a should get incremented after evaluation of expression right. Why is it not happening in this case?
David Byron
Rancher

Joined: Jan 20, 2009
Posts: 169

Here's the proposed program:

The program shown above behaves very much like this one, which clarifies what's happening:

The variable 'a' starts with a value of 2: int a = 2. In the line 'a += a++', the value of 'a' (namely, 2) is assigned additively to the existing value of the variable 'a' (namely, 2), for a new total value of 4. Note that at this point, the assignment of the expression on the right to the variable on the left has already taken place.

Next, the expression on the right is further evaluated (changing from 2 to 3) and then thrown away. Why is it thrown away? Because the assignment of the righthand expression to the lefthand variable has already taken place and there's nowhere else to put the value '3'.

Finally, the current value of the variable 'a' (namely, 4) is printed: 'System.out.println(a)'.

The underlying principle is that there can be only one assignment of value per = operator per expression. Once that condition is satisfied and it's time to increment 'a', there's no place to put the result.

ankit maini
Greenhorn

Joined: Feb 08, 2011
Posts: 25

public class PlusEquals {
public static void main(String args[]) {
int a = 2;

a = a++; //

System.out.println(a);
}
}

Here the result is 2 not 3.
Remember that a++ or ++a both perform first, difference is that a++ returns the previous value so in this case a++ will increase the value of a by 1 means a++ (a=a+1) is 3 but the it returns the previous value which is 2 so 3 is overwrite by 2 and final result is 2.
gaurav gupta sitm
Greenhorn

Joined: Jan 12, 2011
Posts: 22
Thanks David .....
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

Hi,

If you are still questioning the incrementing and decrementing operators, I wrote a little guide on them which may be of interest

Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
anirudh jagithyala
Ranch Hand

Joined: Dec 07, 2010
Posts: 41
Step by step process to evaluate the corresponding expression........
int a=2;
a+=a++;

a+=(a++); //value of a is 2
a=a+(a++); //value of a is 2
//remeber the evaluation of expression is from left to right
a=2+(a++); //value of a is 2
a=2+(2); //value of a is 3 as b=a++; then b=2 and a=3
a=2+2;
a=4

The above process is in java as evaluation of expression is left to right

But in some languages like c,c++ the evaluation of expression is from right to leftthen the output would be 5 as follows
a+=a++;
a=a+(a++); //value of a=2
a=a+(2);//value of a=3
a=3+2;
a=5;

in case of assemblers
a+=a++;
a=a+a++;
a=a+a+1;
a=2+2+1;
a=5;

So the output is 4 in case of Java or .Net and the output is 5 with C,C++ -----

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.

subject: ++ confusion