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
  • 81,630
  • 19
  • 117
  • 142
Ruturaj
  • 570
  • 1
  • 5
  • 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
  • 41,995
  • 16
  • 94
  • 113