Little experiment.
Option 1
public static void main(String[] args) { int x = 1; int y = 0; if (x != 2) { y = 2;} int z = x + y; System.out.print(z); }
No changes. 1 in 1.
Option 2
Source:
public static void main(String[] args) { int x = 1 + 4; int y = 0 + x; if (x != 2) y = 2; int z = x + y - y + y - x + x; System.out.print(z); }
decompiling:
public static void main(String[] args) { int x = 5; int y = 0 + x; if (x != 2) y = 2; int z = x + y - y + y - x + x; System.out.print(z); }
As expected, it took only an obvious addition.
Option 3
But with constants all the more interesting:
Source:
public static void main(String[] args) { final int x = 1 + 4; final int y = 1 + x; final int z = x + y - y + y - x + x; System.out.print(z); }
Decompile:
public static void main(String[] args) { int x = 5; int y = 6; int z = 11; System.out.print(11); }
Here the optimizer has already pulled as he wanted)
JDK Tools 1.7.0.9 + JD 0.6.2
y
not visible outside the block, is it not? - VladD