Java Platform Architecture

Java Platform Architecture

Introduction

A platform is an environment in which programs run. Common examples are Windows, Ubuntu and macOS. The Java platform differs from these platforms because it is a software-only platform running on top of other hardware-based platforms. Java Platform Environment has three main components: JDK, JRE and JVM.

Java Development Kit (JDK)

Java Development Kit is a software development environment provided to develop Java applications and applets. It contains the JRE and various development tools like an interpreter (java), a compiler (javac), a shell environment provider (jshell), a documentation generator (javadoc) and so on.

JDK is the software that is downloaded first for programming in Java. When a particular version of JDK for Windows is downloaded from the official website, a folder of that specific JDK is downloaded at C:\Program Files\Java. The folder structure of JDK 17 (LTS) is as shown below: JDK folder structure

The development tools provided in JDK can be found inside the bin folder, and this is the folder path that is set in the Path environment variable while setting up Java. These tools can then be used directly as shell commands from the command prompt. For example: shell commands

Java Runtime Environment (JRE)

Java Runtime Environment is the part of JDK that builds a runtime environment in which Java programs are executed. JRE consists of the JVM and all the class libraries that are predefined by the Java language. JRE loads the user-defined program, combines it with all the required Java libraries and starts the JVM to execute it.

The pre-defined class and package libraries of Java can be found inside the lib/src zip directory: JRE predefined classes directory

Our most commonly used classes in Java that are automatically called during our program execution are located at C:\Program Files\Java\jdk-17\lib\src.zip\java.base\java\lang

Java Virtual Machine (JVM)

Java is known for its Platform Independence or Architecture Neutrality and this is possible only because of this important component - JVM.

The Java Virtual Machine provides a runtime environment while executing the programs. A JVM is platform-specific, that is, it is different for every operating system it is created for. The platform-dependent JVM converts the platform-independent bytecode (.class files) to the corresponding machine code for executing the same.

The JVM is present as a jvm.dll file at C:\Program Files\Java\jdk-17\bin\server.

Functions of JVM

The JVM performs the following functions:

  • To allow Java programs to run on any device or operating system by doing the following:
    1. Loading the code
    2. Verifying the code
    3. Executing the code
    4. Providing a runtime environment
  • To manage and optimize program memory by providing definitions for
    1. Memory area
    2. Class file format
    3. Register set
    4. Garbage collected heap
    5. Fatal error reporting etc.

JVM Architecture

The internal architecture of JVM is as shown below: JVM architecture

Class Loader Subsystem

The class loader is a subsystem of JVM that loads the .class files into the memory, whenever we run the java program. Then it verifies whether all the byte code instructions are proper or not. If any suspicious instruction is found the execution is terminated instantly. If all instructions are proper, it allocates the necessary memory for executing the program.

There are three built-in classloaders in Java:

Bootstrap Class Loader

This is the first classloader which is the superclass of other classloaders. It loads the src.jar file which contains all class files of Java Standard Edition like java.lang package classes, java.util package classes, java.io package classes, java.sql package classes etc.

Extension Class Loader

This is the child classloader of Bootstrap and parent classloader of System classloader. It loads the required jar files.

System/Application Class Loader

This is the child classloader of the Extension classloader. It loads the class files from classpath. By default, the classpath is set to the current directory. You can change the classpath using the "-cp" or "-classpath" switch.

Runtime Data Areas

Method Area

It is the memory block which stores the class code: code of the variables and code of the methods in the class.

Heap

It is the memory block in which objects are allocated. It is created every time the JVM starts.

Stack

These are the memory areas where Java methods are executed. JVM Stack is known as Thread Stack because separate memory stacks are created for each single execution thread. Each thread stack stores a stack frame and various elements like local variables, partial results, and data for calling methods and returns.

Program Counter Registers

It contains the memory address of the instructions of the methods that are being executed.

Native Method Stack

It contains all the native methods used in the application.

Execution Engine

This subsystem consists of the interpreter, JIT compiler and garbage collector.

Interpreter

Read bytecode stream then execute the instructions.

JIT compiler

The Just-In-Time (JIT) compiler helps in improving the performance of Java applications by compiling bytecodes to machine code at run time. The JIT compiler is enabled by default. The JIT compiler compiles the bytecode of a called method into machine code, compiling it “just in time” to execute it.

Garbage collector

Garbage Collection It tracks every object available in the JVM heap space and removes unwanted ones. Garbage collector works in two simple steps known as Mark and Sweep:

  • Mark – it is where the garbage collector identifies which piece of memory is in use and which is not
  • Sweep – it removes objects identified during the “mark” phase.

Java Native Interface

Java Native Interface (JNI) is a framework which provides an interface to communicate with another application written in another language like C, C++, Assembly etc. Java uses the JNI framework to send output to the Console or interact with OS libraries.

Native Method Libraries

They are libraries written in other programming languages, such as C, C++, and assembly. These libraries can be loaded using the JNI.

Conclusion

Before moving forward with coding in Java, I felt like we should have an idea of the internal structure and working of the language we are working with, hence why this article was written at this point. If you had difficulty understanding this section, it would completely make sense to come back to this after you are comfortable with basic programming.