Second one optimizes to x = 10 via constant propagation.
Third one first unrolls the loop, propagates constants including booleans, and then eliminates dead code to arrive at x = 10.
The last one cannot be optimized as “new” created objects that get used, nextInt() changes the state of those objects, and the global state of the random number system is impacted.
Oddly enough, out of all of these the one the compiler has the best chance of optimizing out is the last one
Not if Random writes to global state, that’s a side effect that must be preserved
What?
First one is optimized obvious.
Second one optimizes to x = 10 via constant propagation.
Third one first unrolls the loop, propagates constants including booleans, and then eliminates dead code to arrive at x = 10.
The last one cannot be optimized as “new” created objects that get used, nextInt() changes the state of those objects, and the global state of the random number system is impacted.
deleted by creator