Reputation: 35
int x = 10;
x += x++;
System.out.println(x);
why the answer of above statement is 20 ?
Upvotes: 0
Views: 510
Reputation: 1449
The operator += is an addition assignment operator. Like Alya said above, x += x++
is equivalent to x = x + x++
, which in your case is x = 10 + 10
. However, it's a very messy statement and I'll explain why towards the end of this post.
Now, you're probably thinking "Why is it 20 and not 21 (10 + 11) since you have the ++?" and that's valid. There's actually a difference between a post-increment and a pre-increment. x++ is the post-increment and will actually evaluate the value of x first and THEN increment x, while ++x is the pre-increment which will increment x and THEN evaluate the value of x.
For example, x = 10; System.out.println(x++); System.out.println(x);
will print 10 and then print 11 because the first print line prints x
and THEN performs the ++ calculation, making x
11 which the next line prints. Conversely, x = 10; System.out.println(++x); System.out.println(x);
will print 11 on both print statements.
Going back to why I said x += x++;
is very messy is because technically the ++ operator isn't performed in this case. x++
is technically the same as x=x+1
and remembering that x+=y is the same as x = x+y) , the line x += x++;
is kind of like saying x = x + (x = x + 1);
which is kind of weird looking because you do 2 assignment statements in one and won't actually "work how you want it". Back to your example int x = 10; x += x++;
if you print x, you will get 20 even though you could look at it as: x is now the value of x + the value of x, then finally + 1 to it. But unfortunately, that's not how it works.
To solve your problem, if you change your code from a post-increment to a pre-increment, then it should work, ie: x+=++x;
will print your 11 but I would argue the that's quite unreadable and a bit confusing. x+=x; x++; System.out.println(x);
is easier to follow.
Upvotes: 3
Reputation: 2570
//
// Shows how increments work.
//
int i = 0;
System.out.println(i);
i++; // Add one
System.out.println(i);
i += 2; // Add two
System.out.println(i);
i += 3; // Add three
System.out.println(i);
++i; // Add one
System.out.println(i);
i += i; // Added itself
System.out.println(i);
//
// Uses increments and assigns.
//
int v = 0;
v = i++; // Increment after value copy
System.out.println(v);
System.out.println(i);
v = ++i; // Increment before value copy
System.out.println(v);
System.out.println(i);
//Output
0 - 1 3 6 7 14 14 15 16 16
Upvotes: 1
Reputation: 832
x+=x++ first assigns the value to x and then increments (post-increment)
x+=++x first increments then assign the value to x (pre increment)
there are two types of increments/decrements in programming
1. pre-increment/decrement
2. post-increment/decrement
In programming both of these have same operations but differ in there nature as they both used for increment or decrement; they can be written as,
x+=1; (increment by 1)
x-=1; (decrement by 1)
you can use a variable instead in the above cases as well
Upvotes: 0
Reputation: 887
So first x
is initialized to be 10
. Then the x++
has higher precedence so that gets carried out first. the "++
" is a post-increment in this case (because it is after the variable as opposed to pre-increment which would be ++x
). Post-increment means "first use the variable then increment it by one" so in this case it first uses x
to be 10
then increments it to 11 after it is used. Then we look at the "+=
" which is short hand for "x = x+x++
". so we have x = 10+10
which = 20
. If you were to carry this out again it would equal x = 20+20 = 40
.
In this particular case, the x++
isn't necessary as x
is reassigned the value after it is incremented each time.
Upvotes: 2
Reputation: 5638
int x = 10; x += x++;
will equal to x=x+x
where x++
mean use the x
value then increament it , so it's value will be 10
so the result will equal 20
if you want to see the change of the x
, see this example:
int x = 10;
int y = 10;
y +=x++;
System.out.println(y);
System.out.println(x);
will print :
y=20
x=11////////////according to x++ and without to overwrite it
Upvotes: 1
Reputation: 9071
x++
will execute first. It returns x
and then increments x
by 1
.
Finally, the +=
operator will add to x
the return value of x++
, which was 10
.
Thus, x
will be 20
and it will overwrite the changes to x
by the statement x++
.
Upvotes: 2