What is JVM, JDK and JRE in Java

Internal details of JVM

What is JVM, JDK and JRE

In this tutorial, I will discuss about JVM and how it works internally? Also, I will discuss what is JDK and JRE and why we are using them ? So let’s get started.

What is JVM

JVM (Java Virtual Machine) is an abstract computing machine which provides run time environment for Java. JVM is a software implementation of a machine that executes programs like a physical machine. Basically, JVM is designed to implement WORA concept “Write Once, Run anywhere”. JVM can run on almost every hardware available in the market to run Java byte codes (code generated after compilation of Java Class file).

To explain about Java Virtual Machine best, let’s take an example of  a movie theater :
  • Directors create movies from different locations like country, cities etc. (You develop code on different environments like Unix, Windows, Mac etc).
  • Movie is recorded and finally distributed to the movie theaters. (You finally compiler your code and provide byte code / class file to JVM).
  • Theaters then provide environment for that movie to run. (JVM takes the byte code / Class file and provide run time environment for that code).

Internal details of JVM

Let’s take a look on internal JVM details as below :

Internal details of JVM
Internal details of JVM

Let’s first understand, What is byte code

Byte code is the middle level language between the user language in which you develop codes and machine language which JVM understands. Basically, this concept was designed to implement “Write Once, run anywhere”.

Byte code is the essential element of JVM. JVM only understands the instructions written in byte code. Java compiler takes file written in Java language then generates byte code instructions (.class file) which JVM understands. Java compiler does not directly generate machine instructions / machine language.

Let’s take a look, how byte code is generated :-

We will now discuss internal architecture of JVM.

Class Loader

Class loader provides dynamic, run time class loading feature. It loads and links the class when it refers to the class for the first time.
  • Class loader loads a class and places it to JVM memory.
  • After loading process, class is then verified. It checks and verifies whether the read class is configured as per the Java Language Specification or JVM standards.
  • After that it resolves and initializes the class variables to proper values.

Class Area Or Method Area

Class area or method area is shared by all of the threads. It is created when JVM actually starts. It stores constant pool, field, method information, static variables etc.

Stack Area

For each thread, separate JVM stack is created. Basically, stack stores stack frames. JVM just performs push and pop operation on JVM stack. A new stack frame is created each time a method is invoked and destroyed after completion. It holds values for the local variables and return values of methods.

Heap Area

A space allocated for the Objects or instances. Heap area is the target for the garbage collection.

PC Register

One PC (Program counter) register is created for each thread. PC register is created when thread starts. PC register holds the address of the JVM instructions being executed.

Native Method Area

This stack is created for the code written in different languages other than Java language. This stack is used by JNI (Java Native Interface) to execute native APIs.

Execution Engine

After all of the above processes, final process is to execute the code. Byte code provided to the JVM then executes it in Execution engine area. Execution engine reads the byte code instructions one by one and generates the code that can be executed by machine.

There are two parts of execution engine as follows :

Interpreter :  Interpreter interprets byte code instructions one by one. Interpreter can interpret the byte code quickly. However, it is slow in execution of interpreted results. This is the main disadvantage of the interpreter.

JIT (Just in time Compiler) : JIT was introduced to overcome the disadvantages of the interpreter. Execution engine runs as an interpreter and also, at the same time JIT reads the all bytecode and converts it to native code. In this way, the interpreter doesn’t need to interpret the code. It just generates the output result.

What is JRE in Java

Java Run Time Environment (JRE) is the used to provide run time environment for the Java programs to run. JRE is the implementation of Java Virtual Machine which physically exists.

Different JREs are available for different operation systems. In order to run java program, you need JRE installed in your machine.

What is JDK in Java

JDK (Java Development Kit) is provided by Java which is used to develop applications in Java. JDK contains set of libraries, APIs and Java Virtual Machine (JRE). A complete package to develop and run Java based applications.

Leave a Reply

Your email address will not be published. Required fields are marked *