I am new to java and struggling to understand the following: Does jit compiles everytime we run the code? (I know jit optimizes that code which is run frequently but I am as
The JIT doesn't remember anything from a previous run.
This means it may compile code every time you run it. The JIT can even re-compile code while it is running to either optimise it further or optimise it differently if it detect how the code is used have changed.
Code which is not considered hot will not be compiled as this is likely to be more expensive than just running it with the interpreter.
When you have tiered compilation (on by default in Java 8) it will compile moderately hot code a little, recompile it more and more as you run it more. It can go through multiple stages.
If you want to see what is being compiled add -XX:+PrintCompilation
on the command line.