java - can JIT optimize such unnecessary call? -


i have question jit optimization. compiled simple piece of code:

class btest {         static final boolean flag=false;          public final void foo(int x)         {                 if(flag) {a=x; b=x*2; c=x*3;}         }          public void bar(int y)  {foo(y);}          int a,b,c; }; 

flag set false foo() compiled empty code - returns. bar() still calls it.

is possible jit eliminate call? matter if flag belongs external class?

regards

it can eliminate , inline in code.

note: can non-volatile non-final variables thinks thread doesn't change value. common mistake like

boolean running = true;  public void run() {      while(running) {         //      } }  public void stop() {      running = false; } 

a common misconception thread might keep running while stop @ unknown point, when jit might inline running , never stop.


Comments