Lately I was discussing Java with few students of mine..
It seems like that for students there is a lot of confusion regarding how Java/The JVM works because there are TWO compilers involve, so when someone mentions a compiler or the Just In Time compiler some of them would imagine it’s the same one, the Java Compiler..
So how does it really works?
It’s well known fact that Java can be written once get compiled and run anywhere (on any platform) which mean that different types of JVM can get installed over any type of platform and read the same good old byte code
2) Upon execution of a Java program (the class file or Jar file that consists of some classes and other resources) the JVM should somehow execute the program and somehow translate it to the specific platform machine code.
In the first versions of Java, the JVM was a “stupid” interprater that executes byte-code line by line….that was extremely slow…people got mad, there were a lot of “lame-java, awesome c” talks…and the JVM guys got irratated and reinvented the JVM.
the “new” JVM initially was available as an add-on for Java 1.2 later it became the default Sun JVM (1.3+).
So what did they do? they added a second compiler.. Just In Time compiler(aka JIT)..
Instead of interpreting line by line, the JIT compiler compiles the byte-code to machine-code right after the execution..
Moreover, the JVM is getting smarter upon every release, it “knows” when it should interpat the code line-by-line and what parts of the code should get compiled beforehand (still on runtime).
It does that by taking real-usage statistics, and a long-list of super-awesome heuristics..
The JVM can get configured by the user in order to disable/enable some of those heuristics..
To summarize, In order to execute java code, you use two different compilers, the first one(javac) is generic and compiles java to bytecode, the second(jit) is platform-dependent and compiles some portions of the bytecode to machine-code in runtime!