Java Core Concepts (JDK, JRE, JVM)

Dulsara Mannakkara
6 min readMar 1, 2022

What is Java?

Java is a general-purpose, class-based, object-oriented programming language designed for having lesser implementation dependencies. t was Layout by James Gosling and released by sun Micro-systems in 1995, but java currently owned by oracle.

Java Development Kit(JDK)

JDK is the software development environment which is used to develop a java program. It consists of the Java runtime environment, libraries, compiler and the interpreter in order to develop a java based program.

Few important components shipped with JDKs are as follows:

· appletviewer — this tool can be used to run and debug Java applets without a web browser

· apt — the annotation-processing tool

· extcheck — a utility that detects JAR file conflicts

· javadoc — the documentation generator, which automatically generates documentation from source code comments

· jar — the archiver, which packages related class libraries into a single JAR file. This tool also helps manage JAR files

· jarsigner — the jar signing and verification tool

· javap — the class file disassembler

· javaws — the Java Web Start launcher for JNLP applications

· JConsole — Java Monitoring and Management Console

· jhat — Java Heap Analysis Tool

· jrunscript — Java command-line script shell

· jstack — utility that prints Java stack traces of Java threads

· keytool — tool for manipulating the keystore

· policytool — the policy creation and management tool

· xjc — Part of the Java API for XML Binding (JAXB) API. It accepts an XML schema and generates Java classes

Java Runtime Environment(JRE)

The Java Runtime Environment (JRE) is a software package which bundles the libraries (jars). To execute any Java application, you need JRE installed in the machine. It’s the minimum requirement to run Java applications on any computer.

JREs can be downloaded as part of JDKs, or you can download them separately. JREs are platform dependent.

JRE bundles the following components –

· DLL files used by the Java HotSpot Client Virtual Machine.

· DLL files used by the Java HotSpot Server Virtual Machine.

· Code libraries, property settings, and resource files used by the Java runtime environment. e.g. rt.jar and charsets.jar.

· Java extension files such as localedata.jar.

· Contains files used for security management. These include the security policy (java.policy) and security properties (java.security) files.

· Jar files containing support classes for applets.

· Contains TrueType font files for use by the platform.

What is Virtual Machine?

A virtual machine is a virtual representation of a physical computer. we are able to call the virtual machine the guest machine, and also the physical computer it runs on is that the host machine.

A single physical machine can operate multiple virtual machines, each with its own operating system and applications. These virtual machines are isolated from one another.

What is Java Virtual Machine(JVM)?

Java uses a combination of both compiled and interpreted languages.

The compiler compiles the Java file into a Java .class file, then that .class file is input into the JVM, which loads and executes the class file. The same class file can be executed on any version of JVM running on any platform and operating system.

Similar to virtual machines, the JVM creates an isolated space on a host machine. This space can be used to execute Java programs irrespective of the platform or operating system of the machine.

Java Virtual Machine Architecture

The JVM consists of,

1. Class Loader

2. Runtime Memory(JVM Memory)

3. Execution Engine

Class Loader

When you compile a .java source file, it is converted into byte code as a .class file. When you try to use this class in your program, the class loader loads it into the main memory.

The first class to be loaded into memory is usually the class that contains the main() method.

The Class loading process contain 3 phases,

1. Loading

2. Linking

3. Initializing

What is Loading

ClassLoader reads the .class file and then the JVM stores the following information in the method area.

The fully qualified name of the loaded class

variable information

immediate parent information

whether it is a class or interface or enum

The three main ClassLoaders in JVM,

1. Bootstrap ClassLoader

The root class loader. It is the superclass of Extension Class Loader and loads the standard Java package.

2. Extension ClassLoader

Subclass of Bootstrap Class Loader and Super Class of Application Class Loader. This loads extensions in standard Java libraries.

3. Application ClassLoader

Subclass of Extension Class Loader. It loads the files in the classpath.

What is Linking

Once a class is loaded into memory, it undergoes a linking process. Linking a class or interface is a combination of different elements and dependencies of the program.

This process divide into 3 parts.

1. Verification

This phase checks the structural correctness of the .class file by checking it against a set of constraints or rules. If verification fails for any reason, we receive a VerifyException.

2. Preparation

Allocates memory for static fields in a JVM class or interface, and initializes with the default values.

3. Resolution

Symbolic references are replaced with direct references in the running time constant pool

What is Initializing

Initialization is the implementation of the class or interface Initialization method. This may involve calling the class constructor, executing the static block, and assigning values to all the static variables. This is the final stage of class loading.

Memory Area

The memory in the JVM is devided in to five different parts,

  1. Method area
  2. Heap
  3. Java Stack
  4. PC Registers
  5. Native Method Stacks

Method Area

The method area stores the class code − code of the variables and methods.

Heap Area

The Java objects are created in this area.

Java Stack

While running methods the results are stored in the stack memory.

PC Registers

These contain the address of the instructions of the methods.

Native Method Area

native methods are executed on the Native method stacks.

Execution Engine

After loading the byte code into the main memory and retrieving the runtime data area, the next step is to execute the program. The Execution Engine handles this by executing the code present in each class.

However, before the program execution, the bytecode must be converted into machine language instructions. The JVM can use an interpreter or a JIT compiler for the execution engine.

Execution Engine has three main components,

1. Interpreter

2. JIT Compiler

3. Garbage Collector

What is Interpreter

This is responsible for converting bytecode into machine code. The interpreter reads and executes the bytecode instructions line by line. Due to the line by line execution, the interpreter is comparatively slower. The main disadvantage of Interpreter is when a method is called multiple times, every time a new interpretation is required and this will reduce the performance of the system. So this is the reason where the JIT compiler will run parallel to the Interpreter.

What is JIT Compiler (Just In Time Compiler)

The JIT Compiler overcomes the disadvantage of the interpreter. The Execution Engine first uses the interpreter to execute the byte code, but when it finds some repeated code, it uses the JIT compiler. The JIT compiler then compiles the entire bytecode and changes it to native machine code. This native machine code is stored in the cache and used directly for repeated method calls, which improves system performance.

What is Garbage Collector

The Garbage Collector collects and removes unreferenced objects from the heap area. It is the process of reclaiming the runtime unused memory automatically by destroying them. Garbage collection is done automatically by JVM and does not require separate handling.

References,

What is JVM — Java Virtual Machine - https://youtu.be/bUtIIWbaFKc

https://www.freecodecamp.org/news/jvm-tutorial-java-virtual-machine-architecture-explained-for-beginners/

--

--