6
System.out.println(info + ": " + ++x);

is this statement equivalent to

x++;
System.out.println(info + ": " + x);

and

System.out.println(info + ": " + x++);

is equivalent to

System.out.println(info + ": " + x);
x++;

As JVM can only process one statement at a time, does it divides these statements like this?

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
Ruturaj
  • 630
  • 1
  • 6
  • 20

1 Answers1

3

Yes and yes.

++x will be executed before the containing statement, ie the value of x will be incremented before it's used.

x++ will be executed after the containing statement, ie the value will be used and then the variable x incremented.

To be clear: in both cases the value of variable x will be changed.

Szymon
  • 42,577
  • 16
  • 96
  • 114