Java architecture and its components


August 23, 2021, Learn eTutorial
1727

In this tutorial, we will be discussing java architecture and its several components. You will master the three important java components - JVM, JRE, and JDK. This includes their working principle and how they differ from each other.

Java Architecture

Architecture is a basic plan of all the components that collectively define the system comprising the structure, the interfaces, the modes of communication, etc. The comprehensive architecture of java is the fusion of multiple components . Below shows an outline sketch of java architecture.

Java JVM

The first and foremost component of Java is the Java application which comprises another two components - the java programming file and the java class file. The java class files are the files that get created on the compilation of the Java source file. Here the source code is converted into a machine-independent byte code. These bytecodes are then passed to Java Runtime Environment(JRE) which is yet another important component of java. The byte codes now integrate with the class libraries and the Java Virtual Machine interprets these byte codes and translates them to native machine code. Java, thus, embodies both the techniques of compilation and interpretation.

What exactly is JVM?

JVM  stands for Java Virtual Machine. As its name suggests, JVM behaves as a virtual processor and is one of the core elements of java. The main objective of this virtual processor is to interpret the byte codes and convert them into native machine code.

Java JVM

The position of JVM in Java Architecture is highly remarkable. As you can see in the above diagram, JVM is a part of JRE and is placed on the top of the operating system. This enables JVM  to easily interact with multiple operating systems which makes java so powerful and portable. 

Working of JVM

Now let's dive deep into the working system of Java Virtual Machine. When you observe the diagram of Java architecture, you can see that the JVM is built of 3 main subsystems. 

  1. Class Loader Subsystem
  2. Runtime Data Area (Memory Area)
  3. Execution Engine

1. Class Loader Subsystem and its working

Class loader subsystem is an inevitable component of Java Virtual Machine. The responsibility of a class loader subsystem is not limited to simply finding and loading class files to memory, despite being pledged for the verification of exactness of the imported class files, for allocation and initialization of memory for class variables, and many more. All these activities are achieved through 3 processes namely

  • Loading 
  • Linking
  • Initialization.

Loading 

This is the first phase of the classloader subsystem which is obligated to perform two functions. Firstly to find the byte code file requested by the VM for a particular class and secondly to load these  bytecodes to the main memory. Class loaders are categorised into three types based on the class type and class path. They are:
BootStrap Loader

  • Extension ClassLoader
  • Application ClassLoader

Linking

This middle phase is responsible for linking the class files with the runtime state of JVM for execution. This phase begins with the process of verification, where the byte code verifier ensures that generated bytecode is proper in the sense it adheres to the semantics of the language and does not disturb the integrity of JVM.
The very next step is a preparation where the JVM allocates memory for all static variables and assigns them with default values observing the variable type.
The linking phase ends with an optional process called resolution where JVM replaces all the symbolic references of classes, interfaces, methods, etc with the concrete references from the Method Area.

Initialization

As stated in Java 8 Virtual Machine Specification, initialization of a class or interface consists of executing its class or interface initialization method.
On reaching this final phase of class loading the class is ready for its active participation in the application. Initialization involves two main functions

  1. Firstly initialize all the class variables with original values with the aid of some initialization routine outlined by the programmer.
  2. Secondly, if the superclasses of the class are found uninitialized then they must be initialized

2. Runtime Data Area

Runtime Data Area or the Memory Area is the second important section of a Java Virtual Machine. The java memory is mainly divided into five parts. These are :

  • Method Area:  Also known as the class area is the memory where class bytecodes are loaded into along with static variables and constants. Since a JVM contains only one method area it is shared across the resources.
  • Heap:  This is the memory where objects of classes and arrays are allocated and is common and shared across multiple threads. Objects are the instance variables created under the object name. 
  • Stack: In simple language, this memory area stores the methods as a frame and performs the execution. On method invocation, a frame is formed and after execution or completion of this method, the frame gets destroyed. This is also nested for local variables.
  • PC Registers: PC stands for Program Counter. In computer architecture, a Program Counter Register is used to keep a track of current instruction executing at present. It contains the address of the currently executing instruction and hence acts as a pointer. We know that java is a multithreaded system, so every time a thread is created correspondingly a PC register is formed.
  • Native Stacks: Native method stacks are used in situations where JVM uses the native methods. It contains the address of currently executing an instruction that is native to the system. 

3. Execution Engine 

The execution engine is the core part of Java Virtual Machine where the execution of bytes codes takes place. It is this phase that translates human-readable codes into machine language executable codes.

The execution engine consists of three main sub-components. They are:

  • Interpreter: This component in the java virtual machine is responsible for the reading and execution of java programs. It reads the high-level codes line by line and translates them at runtime to a high-level language and thereby ensuring easy execution of programs. However, it has a limitation of repeatedly converting the same method that appears multiple times in the code.
  • JIT Compiler: JIT stands for Just-in-time. This JIT compiler is responsible for balancing out the limitation of slow execution caused by the interpreter. It improves the performance by compiling the byte codes dynamically causing a decline in the total time required for compilation. Compiler here refers to the translator that performs complex optimization to create high-quality machine-readable code
    • Intermediate Code Generator – generates an independent intermediate representation of code
    • Code Optimizer – Optimizes the intermediate code aimed with an aspect to work efficiently and effectively with fewer resources
    • Target Code Generator – Generates machine-dependent codes.
    • Profiler – Responsible for locating hotspots, determines if a method repeats multiple times.
  • Garbage Collector: This sub-component in Java Virtual Machine helps the Java developers not to be overwhelmed with having to free up the memory explicitly. The garbage collector automatically detects the unused or unreferenced memory and deallocates them for later use thus making java memory-efficient.

Java Native Interface(JNI)

The JNI is a non-java programming interface that is used in situations where you can’t entirely depend on java for writing your application. At times you may need to incorporate methods or libraries written in other languages like C, C++, etc to run your application. In such cases, you can use this native method interface which allows the java codes that execute inside the JVM to work together with applications and libraries written in other languages. The main advantage of using this JNI framework is the code reusability and high performance.

Native Method Libraries

Java Native Libraries are libraries that contain codes or applications written in non-java languages for instance C, C++, etc. They can be included in the java application as per the requirement with the help of JNI.

What Is JRE?

JRE stands for Java Runtime Environment. As its name suggests, it is mainly used for running the java code. JRE contains the Java Virtual Machine which we discussed earlier and other browser plugins and tools that support the running of java applets. JRE is one of the important parts of JDK.

What Is JDK?

JDK, short for Java Development Kit, is a feature-rich software development environment primarily used by java programmers for developing java codes(java applications or applets). It comprises various tools needed for building and executing java codes like 

  • JRE (Java Runtime Environment) 
  • An interpreter (java)
  • A compiler (javac)
  • An archiver (jar)
  • A documentation generator (javadoc) etc.

So if you're a novice to JAVA, let's make it clear that to develop a java code in your system, the first and foremost step you need to undergo is to download the JDK  into your development environment and establish the path. While downloading you can choose the desired java version and java edition. Java editions are of 3 types: 

  1. Java Standard Edition (Java SE)
  2. Java Enterprise Edition(Java EE)
  3. Java Mobile Edition (Java ME)

Don’t bother yourself too much about the installation of java, you will learn it step by step in our next tutorial-JAVA INSTALLATION AND SETUP FOR WINDOWS.  

JVM vs JRE vs JDK

Now let’s sum up everything we have learned so far by comparing the 3 essential components. Below is the best visualization referring to the correlation of JVM, JRE, and JDK.

Java JVM JDK JRE

The difference is listed in the table below:

  JDK JRE JVM
Stands for Java Development Kit Java Runtime Environment Java Virtual Machine
Is a software development tool Software package Abstract machine
Used for Developing applications Running application Interpretation
Dependence Platform dependent Platform dependent Platform independent
Contains JRE, JVM, Class libraries, compilers, debuggers, etc JVM, class libraries Subset of JDK and JRE