JVM stands for Java Virtual Machine. Every Java program which you are going to develop has to go from JVM. It is a specification that provides a runtime environment for executing Java program.

As an aspiring Java developer, you need to understand what is happening behind the scene. Java is also known for its two-stage compilation and interpretation process.

What is Bytecode?

When you write a program in Java and compile it, after compilation, it results in bytecode instruction specifications provided in a file with extension .class 

Bytecode format is halfway between the code that programmer writes and the code that the computer requires.

These bytecode instructions are then interpreted by JRE i.e. Java Runtime Environment. As discussed in our previous post overview of Java programming, Java is a platform independent language which means suppose any program which you develop on any OS assume Windows, same program which you developed on Windows will be compatible to run on Linux and Mac as well. This concept is also known as WORA (Write Once Run Everywhere).

Initially, James Gosling started modifying and developing C++ compiler to make it platform independent but it wasn’t a right approach. Then he created Java interpreter which is used to interpret Java bytecode for execution. Java is portable because of JVM. Portability of Java program is due to its bytecode magic.

These bytecode instructions can be interpreted on any of the machines which are having Java Runtime Environment.

Following diagram shows the flow of compilation and execution of a program.

jvm

Fig. Process of compilation and execution

Java Virtual machine performs various operations behind the scenes.

It loads code, verifies the integrity and security of code and executes it. It also provides a runtime environment for a particular set of hardware and hence making independent of hardware and Operating System used.

What is JRE?

According to Wikipedia, Java Runtime Environment (JRE) is a software package that contains what is required to run a Java program. It includes a Java Virtual Machine implementation together with an implementation of the Java Class Library. The Oracle Corporation, which owns the Java trademark, distributes a Java Runtime environment with their Java Virtual Machine called HotSpot.

Java Development Kit (JDK) is a superset of JRE provides all the tools for a Java programmer such as javac i.e Java compiler and jdb i.e. Java Debugger.

Note – Java program is platform independent but JDK itself is platform dependent. JDK is different for the different architecture. Every JDK has different internal system calls for different target operating system.

Also read: How to install JDK on Windows or Linux?

Architecture of JVM

JvmSpec7.png
By Michelle RidomiArchitecture of JVM, CC BY-SA 4.0, Link

Above diagram shows the architecture of JVM and consist of following components.

Classloaders :

Classloader is used to load the java classes into the memory. It performs three task

  1. Loading – find and load binary data of type
  2. Linking – performs verification, preparation and resolution(transforming symbolic reference into direct reference)
  3. Initialization – Invokes the Java class file that initialises the class variables

Bootstrap class loader and User-defined class loader are the two types of class loaders. Every JVM has Bootstrap class loader which loads trusted class files.

Method Area :

Method area is a memory which is also known as class area. This memory is shared between all the threads of JVM. It stores the method data and code for constructors and methods in the class.

Heap :

Heap is a memory which is also shared between all JVM threads. Logically method area is a part of heap memory. Heap allocates the runtime memory for the class instances and the arrays defined in the programs.

JVM language stack :

JVM language stack is a data structure which works in Last In First Out manner(LIFO). It plays a very important role in method invocation and its return values. Each thread has a private JVM stack. Java stack stores frames. It stores local variable and partial results.

PC registers :

PC stands for Program Counter. PC registers hold the address of the instruction currently being executed. If the method is not native then PC registers hold the address of the current instruction which is being executed. If the method is native then the value of PC register is undefined.

Native method stacks :

JVM consists of the native method stacks to support the native methods for C stacks.

Execution Engine :

The execution engine is capable of interpreting bytecode which is generated in the .class file after compilation of Java source code. It also consists of Just In Time (JIT) compiler.

What is JIT compiler?

JIT stands for Just In Time. JIT compiler allows the Java environment to compile the Java bytecode as needed. Unlike another language which converts all source code into the object code once, JIT compiler compiles the block of bytecode which is needed at runtime.

This approach increases the performance of the Java program and decreases the CPU load.

Stay connected with us. Thank you 🙂

Related articles:

How to install and setup Java environment on Windows or Ubuntu operating system?

Overview of Java programming language (Learn Java)


1 Comment

cheat ros android · February 18, 2018 at 8:56 pm

Enjoyed reading through this, very good stuff, thankyou .

Leave a Reply

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