3

This question is just curiosity : I was wondering what would be the value of some int x after the line x += ++x So I tried that :

int x=10;
x+=++x;
System.out.println(x);

And it printed out :

21

After some tests with other values, it seems to be equivalent to x=2x+1. Why ? Is this line interpreted by the compiler as a byte operation ? (By the way, x += x++ seems to be equivalent to x=2x).

I don't think it's something I'd ever use in a project, but I'm curious to know why I get this result.

Thanks for any explanation or hint

EDIT : First of all, thanks for your answers

I knew how the += operator works, as well as the x++ and ++x, but for some reason the (completely logic and obvious) result seemed strange to me I should probably have thought it through, sorry for your time !

Dryster
  • 33
  • 5

6 Answers6

9

The way it is calculated is

  • Step 1: x = x + ++x
  • Step 2: It become x = 10 + (incremented x) 11

  • Step 3: Final result stored in x i.e. 21

Here is the proof:

I created a MainClass as below:

public class MainClass{
public static void main(String...s){
int x = 10;
x += ++x;
}
}

and then checked the bytecode using javap -c MainClass

  public static void main(java.lang.String...);
    Code:
       0: bipush        10     // push 10 onto stack
       2: istore_1             // store 10 in local variable 1
       3: iload_1              // load local variable 1 (now 10) back to stack
       4: iinc          1, 1   //increment local variable 1 by 1
       7: iload_1              // load local variable 1  (now 11) back to stack
       8: iadd                 // add top 2 variable on stack ( 10 and 11)
       9: istore_1             // store 21 to local variable 1
      10: return
}
Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39
2

Its about operator precedence and how ++x and x++ are evaluated and used. with ++x, the value of x is incremented and then used so ++x becomes 11 and this x += ++x becomes 21 which is 10 + 11

However x++ says x is used and then its value is incremented

so x+= x++ will mean 10 + 10 i.e 20

Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
0
int x=10;
x+=++x;
System.out.println(x);

x + = ++x is evaluated in the compiler to x = x + ++x => x = 10 + ++x => x = 10 + 11 => x = 21

Sam Denty
  • 3,693
  • 3
  • 30
  • 43
0

See here:-

x+=++x; this expression will be executed like x=x+(x+1) so x = 10 + 11

hence x = 21;

You need to understand about pre-increment(++x) and post-increment (x++). see below

 int x = 10;
 if (x++ == 10 )
     System.out.println( "X is equal to 10");// this statement will print

in the above if condition it will execute as true because first it will compare as is 10 == 10 and then x will be incremented by one and x will become 11.

Now see below:-

   if (++x == 10 )
     System.out.println( "X is equal to 10");// this will not print if condition will tern false

In the above if condition x will be pre-incremented so x will become 11 and then a comparison will be done whether 11 == 10 hence if condition will failed.

Hope this will help.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
0

++x will return value of (x+1), and x value will be increased by one too.

x++ will return value of (x), and x value will be increased by one too.

so

x+=++x is the same x=x+(x+1), which is equivalent to x=2*x+1

x+=++x is the same as x=x+(x), which is equivalent to x=2*x

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
0

.....++x: first calculate x=x+1, then use x for comparing or calculating the actual task .....x++: first compare / calculate actual task, after that calculate x=x+1