javaoptimization

JIT compiler and anonymous inner classes


The callback functions by anonymous class is a common approach when we use a framework or a library, so it is very useful to know if the JIT performs such kind optimizations.

I am wondering if the JIT compiler is smart enough to understand that them some code is executed every time and compile into native?


Solution

  • Like any other piece of code, it will be compiled and optimized by the JIT compiler once it has been executed enough times (by default, 10000 with the Hotspot server VM, 1500 with the client VM), so the JIT has enough data to base its optimizations on.

    Just because it's anonymous does not mean it's treated any different: it's still a class, with a name generated during the compilation (MyClass$1).