Java command execution order

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

is a statement equivalent to

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

and

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

equivalently

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

Since the JVM can only process one statement at a time, does it divide these statements as follows:

+6
source share
1 answer

Yes and yes.

++x will be executed before the containing statement, i.e. the value of x will be increased until it is used.

x++ will execute after the containing statement, i.e. the value will be used, and then the x variable is incremented.

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

+3
source

Source: https://habr.com/ru/post/956808/


All Articles