Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java complex statement execution order

Tags:

java

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?

like image 998
Ruturaj Avatar asked Oct 27 '13 04:10

Ruturaj


People also ask

What is the order of execution of program statements?

Execution always begins at the first statement of the program. Statements are executed one at a time, in order, from top to bottom. Function definitions do not alter the flow of execution of the program, but remember that statements inside the function are not executed until the function is called.

Can we change order of try catch and finally?

Normally order execution order of try-catch-finally is first try , then if exception trows and caught will execute the catch . If exception caught or not finally will always execute. If return in your try , execution in try will stop there and will execute finally .


1 Answers

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.

like image 89
Szymon Avatar answered Oct 27 '22 00:10

Szymon