Java – Interpreted or Compiled ?
Well the answer is not so simple, but we will have one by the end of this short article🙂
We all know that the source code of a Java file is ‘compiled’ into a .class file.
I put compiled in quotes because the .class file that results is not actually the machine code executed by the machine
The .class (JVM byte code) file might be seen as an intermediary language that might be even modified before is interpreted , compiled and executed.
This happens very often in an JEE environment and it is called weaving.Weaving is a technique of manipulating the byte-code of compiled Java classes.
After we have the final version (might be directly and not necessarily weaved),the byte-code is interpreted and the code is executed .
So where the code is then actually compiled !?
Well an interpreter can generate machine code to get a job done faster if this is important.
A modern VM will use all kind of techniques to do it faster and efficient. For example, for each instruction in the intermediate language the VM could ask which is going to be quicker: to call a routine or to generate machine code to get the job done. This approach is often called Just-In-Time or JIT compilation. It is usually explained as the VM compiling the intermediate language just before it is run, but this isn’t really a good way to think of it.
So the JIT is a sort of mixture of interpreting the code and compiling the code according to what makes best use of the real machine.
To respond to the initial question : it is both !
A diagram that describe the above: