What is Java Virtual Machine (JVM)?

The Java Virtual Machine (JVM) is a virtualized computing environment that enables Java applications to run on different platforms without needing to be rewritten. The JVM acts like a translator, converting Java code into a format your computer can understand and run. This platform independence is a hallmark of Java and has contributed to its widespread use in web applications, mobile apps, and enterprise software.

Imagine a translator who ensures that a book written in one language can be read anywhere in the world. The JVM plays a similar role, allowing Java programs to run seamlessly across different platforms. Let's discuss first how computers run programs:

How computers run programs

As developers, we often use high-level languages like Java, PHP, JavaScript, etc., to write our programs.

In computers, the CPU is the component responsible for executing or running instructions we write. Unfortunately, it does not understand any high-level languages.

The only language that the CPU can understand is the machine language. It looks like this:

000101110011
11001111011000
010110010110110

As you can see, it is a sequence of binary instructions (0s and 1s) which are executed by a computer’s processor.

One other thing you need to know about machine language is that it is tied to the hardware. So, each computer has its own machine language.

Given the complexity of machine language, we build translators that convert programs written in a high-level language to machine language.

The JVM in a nutshell

Now that we have a good understanding of how a computer executes a program, let’s discuss the Java virtual machine.

The first thing to know is that JVM is a computer (or a machine), but not your typical machine. This computer does not exist as actual hardware, and does not even have an operating system. It is a hypothetical computer platform.

Next, as discussed above, a computer needs a machine language to execute programs, so the JVM also has its own machine language which is called Java bytecode. It is the role of the JVM to convert the bytecode into machine language for the actual computer (or hardware).

The JVM is part of the Java Running Environment (JRE).

Note: The JVM was originally for the Java language, but it has evolved to support many other languages like Scala, Groovy, and Kotlin, mainly.

In short, this is how it works:

How does the JVM work?

The JVM works in three primary stages:

  1. Compilation: Java source code (.java) is compiled into bytecode (.class file) by the Java Compiler (javac). The bytecode is a platform-independent code that can be executed by any JVM.

  2. Loading: The class loader in the JVM loads the compiled bytecode into memory.

  3. Execution: The bytecode is executed by the JVM’s interpreter or Just-In-Time (JIT) compiler, which converts bytecode into native machine code specific to the underlying platform.

So, once a Java program is compiled, it can be run on almost any computer as long as it has a JRE (which also simulates the JVM). This is why the Java slogan is:

Write once, run anywhere (WORA)

Let’s see it in practice. Make sure you have a Java programming environment and that you can access these commands:

  • javac (pronounced “java-see”): the Java compiler
  • java: the Java bytecode interpreter
From Java source code to native machine code
  1. Create a file with your preferred code editor and call it HelloWorld.java. Paste the following code snippet in the newly created file:
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}
  1. Now, you can open your terminal (or cmd on Windows). Move to your HelloWorld.java file and type:
javac HelloWorld.java
  1. This command will generate a bytecode file called HelloWorld.class. To interpret this file, you just need to run:
java HelloWorld
  1. That’s all. You can move HelloWorld.class onto any computer and execute it as above.

class HelloWorld {
public static void main( String args[] ) {
System.out.println( "Hello World!" );
}
}

Try to change the "Hello World" with your name, execute the code by clicking the "Run" button.

Now we are familiar with the working of JVM, let's explore its key components:

Key components of the JVM

The JVM is made up of several important components that work together to execute Java programs:

  • Class loader: The class loader is responsible for loading class files (bytecode) into memory from various sources such as the file system or network.

  • Bytecode verifier: This component ensures that the bytecode is valid and doesn't violate any security constraints. It checks for access control, stack overflow, and memory corruption issues.

  • Interpreter: The JVM interpreter reads bytecode instructions and executes them one by one. It's a slower method of execution, but it's cross-platform and easy to implement.

  • Just-in-Time (JIT) compiler: The JIT compiler optimizes performance by converting bytecode into native machine code at runtime. This reduces the need for repeated interpretation, thus improving speed.

  • Garbage collector: The garbage collector manages memory by automatically reclaiming memory used by objects that are no longer referenced. This reduces the likelihood of memory leaks.

  • Execution engine: The execution engine is the component responsible for executing the bytecode. It can either use the interpreter or the JIT compiler to run the program.

  • Runtime data areas: The JVM has various memory areas like the heap (for dynamic memory allocation) and the stack (for method calls and local variables) that help in managing data during program execution.

Unlock your Java potential and take control of your coding journey! Start learning Java today to explore other than JVM, build Java-based applications, and become a Java developer now!

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is the role of the class loader in JVM?

The Class Loader in the JVM is responsible for loading class files (bytecode) into memory at runtime. It reads .class files from different sources like the file system or network and loads them into the JVM to be executed. It ensures that the classes are loaded when needed and maintains the classpath.


Is JVM a software or hardware?

JVM is software, specifically a virtual machine software that runs on a physical machine (hardware) to provide an abstraction layer for executing Java bytecode.


What are the three components of JVM?

The three main components of the JVM are:

  • Class loader: Responsible for loading class files into memory.
  • Execution engine: Executes the bytecode, either through interpretation or JIT compilation.
  • Garbage collector: Manages memory by cleaning up unused objects.

What are the four main tasks of JVM in Java?

The four main tasks of the JVM are:

  1. Loading: Loading bytecode into memory.
  2. Verification: Verifying the bytecode for security and correctness.
  3. Execution: Executing bytecode either via interpretation or Just-In-Time (JIT) compilation.
  4. Memory management: Managing memory, including garbage collection.

How does JVM handle memory management?

JVM handles memory management using automatic garbage collection. The garbage collector reclaims memory used by objects that are no longer referenced, thus preventing memory leaks. The JVM also divides memory into areas like the heap, stack, and method area for efficient memory allocation and deallocation.


Does JVM offer any security features?

Yes, JVM offers several security features, including bytecode verification, security managers, and class loaders. These features prevent malicious code from violating security policies by verifying bytecode and controlling access to system resources.


Free Resources