JDK vs JRE vs JVM

In this module we will learn the key differences between JDK, JRE and JVM as this is one of the most popular interview question.

Table of Contents

[hide]
  • Java SE Development Kit (JDK)
  • Java Runtime Envirnment (JRE)
  • Java Virtual Machine (JVM)
  • Just in Time Compiler (JIT)

Java SE Development Kit (JDK)

JDK is an abbreviation for Java Development Kit. We need Java SE development kit to develop and run java applications on the system. It is the development environment for software developers which help them in building applications.

It contains JRE and development tools for developing, debugging, and monitoring java code. JDK is Platform Dependent. It comes with the installer.

It is possible to install more than one JDK version on the same system . It can be installed on Windows, Unix, and Mac operating systems.

JDK = JRE + Develpment Tools

jdk

Here are the main components of JDK:

JRE

JRE is the implementation of JVM,it provides a run time environment for the execution of Java programs.

Java Class Libraries (JCL)

The Java Platform provides a robust collection of standard class libraries,including common functions for modern operating systems. They are called at Run Time.

Javac (Compilers)

A Java compiler is a program that takes the .java source file and compiles it into a platform-independent .class java file called bytecode.

Debuggers

Programmer can develop and test their code in debugger,it serves its purpose and takes less time to get to the root of a problem.

Java Archive (JAR)

A JAR file is a compressed version of.class files,audio files,image files and folders.

Java RunTime Environment (JRE)

Java RunTime environment is a set of software packages that provide JVM and other class librares that are required for application to run. If you just want to run a java code, you can easily do it with JRE, but it cannot develop or write the java program.

The Java Runtime Environment (JRE) provides a runtime environment and is the implementation of the JVM. JRE is a part of JDK as shown in the abouve diagram.

It uses important package classes such as math, util, lang and runtime libraries. If you need Java applets to run, then JRE needs to be installed in your system.

JRE Workflow

javacompilerimage

As you can see from the above diagram that Compiler converts the java source code into Simple.java file to Simple.class file known as ByteCode. Java Bytecode is a machine code/software in the form of .class file that can run into multiple platforms with the help of JVM.

jre runtime

Here are the main components of JRE:

Class loaders

The Java ClassLoader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. Mainly JVM uses three class loaders i.e bootstrap class loader,extensions class loader,and system class loader.

Byte code verifier

The bytecode verifier checks if the bytecode provided is valid and do not breach java's security restrictions. The JIT (Just-in-Time) compiler compiles the program one bytecode at a time and runs the program.

Interpreter

Once the classes are loaded and the code checked,the interpreter reads the code line by line.

Run-time

Run-time is described as the interval of time during which the particular program is running.

Hardware

Once Java code is compiler,it is ready to run on a specific hardware program.

Java Virtual Machine (JVM)

JVM is a sofware that reads Bytecode and converts it into the OS understandable language, it resides in the real machine and provide the runtime environment in which Java code can be executed.

JVM comes with JIT(Just-in-Time) compiler that converts Java source code into low-level machine language. It compiles the program one Bytecode at a time(line by line) and runs the program.

JVM is customizable, we can easily allocate memory to it. It is independent of the hardware and the operating system. So, we can write Java program once and run anywhere.

JVM Architecture

jvmimage

Java Class Loader

The Class Loader loads all necessary classes needed for the execution of a program. Following are the steps :-

1- LOADING: It find and imports the binary data for a particular class. It is done by BootStrap ClassLoader, Extension ClassLoader, and Application ClassLoader.

2- LINKING: It mainly focuses on verification of imported type, allocating and initializing the memory.

3- INITIALIZATION: This is the final phase of ClassLoading; here, all static variables will be assigned with the original values, and the static block will be executed.

Runtime Data Area

Runtime Data area has five main components as shown in the diagram.

Method Area

JVM has a method area common across all the threads. There is only one method area per JVM, and it is a shared resource.It contains per-class elements such as constant pool, fields, local data method, method code, constructor codes, etc. that are used in the class and initialization of objects / interfaces.

Heap Area

All Java objects are allocated in the Heap, the heap is shared by all Java threads. There is also one Heap Area per JVM. Java applications cannot explicitly free an object, garbage collector in invoked time to time for this purpose.

Stack Area

Java stack consists of Frames that contains the state of one Java method invocation. Frames has two parts local variable and operand stack.Each Java thread has its own stack and cannot access the stack of other threads. JVM has no Registers, it uses the operand stack for storing data values.

PC Registers

Each thread will have separate PC registries, to hold the current execution instruction address once the instruction is executed, the PC registry will be updated with the next instruction.

Native Method Stack

This Stack holds the native method information. For each thread a separate native method stack will be created.

Java Execution Engine

Java Execution Engine executes java Bytecodes either using interpreter or Just-In-Time compiler.

Interpreter

Interpreter is basically a translator which converts Java Bytecodes into machine understandable format. It reads input from the source program and translate it instructions by instructions. Execution of interpretor is very slow as compared to native method execution.

Just-In-Time (JIT)

JIT dynamically compiles bytecode into native code at runtime. Execution on native code is much faster than the execution done by interpreter. Thus it improves performance.

It is one of the most integral part of Java Runtime Environment. It is mainly responsible for performance optimization of Java-based applications at run time or execution time.

Working Flow

When we write Java program, JRE uses javac compiler to compile source code .java file to ByteCode .class file. JVM loads the Byte Code at Runtime and converts the bytecode into machine understandable format i.e binary code. It is then executed by interpreter.

As told earlier execution by interpreter is slow, hence it reduces the performance when compared to native application. Thats when JIT came into picture to boost up the performance.

When any method is invoked in java, JIt compiler is automatically enabled. When method is compiled JVM invokes the compiled method code without interpreting thus it does not require much memory and processor time.

Java Native Interface (JNI)

An interface that allows Java to interact with code written in another language is know as JNI. We can reuse exixting code with java mostly C/C++.

The java command-line utility is an example of one such application, that launches Java code in a Java Virtual Machine.

JNI is a framework that helps to communicate with different applications in another languages. JNI is not portable.