21 June 2013

Difference between JVM and JRE

The basic difference between the JRE (Java Runtime Environment) and JVM (Java Virtual Machine) is, JVM is a specification and JRE provides the implementation.

In simple
JRE = JVM+ java and native API's.

Look at the following diagram for an idea of what JRE is.

The above is the overview of JRE. 

JVM is the real machine (piece of software) which is part of the JRE that executes the byte code.

In java first the source file (*.java) is compiled into the byte code by using a compiler that is "javac" which is provided as part of the jdk. Then to run this byte code (*.class files) the class file should be supplied to JVM. This is done by invoking "java" tool which is provided as part of the jdk. 

The following diagram is the architecture of the JVM:-

So once the byte code is supplied to JVM, it does the following steps
  •    The class loader loads the class file and it makes it available to the JIT (Just Intime Compiler) or the Interpreter which is part of the JVM.
  • JIT or Interpreter then converts the byte code to the machine code (Executable code) and stores it in class area. That means JIT executes the byte code.
  • while execution JVM uses other classes that are part of the program from the API's provides by the JRE , the method variables are stored on the stack and objects are stored on heap.
  • JVM uses the PC registers to maintain current and next executable instructions.
This is how the JVM works in brief.

One important thing to know is the difference between java compiler and JIT. Java compiler is a tool (piece of software) provided as part of jdk and is used to convert the source code to byte code but the JIT is part of the JVM that converts the byte code to executable code (direct instructions to CPU).
Post a Comment