What is Java virtual machine (JVM)?

Java is a popular, object-oriented programming language that is platform-independent. A Java program can nearly be run on any device. This is made possible thanks to the Java virtual machine (JVM).

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 CPU - the brain or heart of a PC

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

000101110011
11001111011000
010110010110110

As you can see, it is a sequence of zeros and ones which are mechanically executed by a computer.

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:

  • A Java program (.java) is compiled to Java bytecode program (.class).
  • The Java bytecode program is interpreted to a machine language program for the actual computer.

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

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!");
  }
}

Now, you can open your terminal (or cmd on Windows). Move to your HelloWorld.java file and type:

javac HelloWorld.java

This command will generate a bytecode file called HelloWorld.class. To interpret this file, you just need to run:

java HelloWorld

That’s all. You can move HelloWorld.class onto any computer and execute it as above.

Advantages of the JVM

The JVM appeared in 1995. It has introduced two revolutionary concepts:

  • Write once, run anywhere (WORA)
  • Automatic memory management

At the beginning of this shot, we said that Java is a platform-independent language. It’s one of the selling points of Java: it can actually run on any computer.

This is made possible because the compiled Java program is not intended to be directly executed by the actual hardware, but by the virtual computer. So, once a computer has the Java bytecode interpreter, it can run any Java bytecode program, and the same program can be run on any computer that has such an interpreter. Amazing!

Note: While Java is platform-independent, the JVM is not. Each operating system has its own JVM implementation.

The second advantage that you as a Java programmer should rejoice in is how the language manages program memory for you. Inside a running JVM, there’s a process called garbage collection, which is in charge of tracking memory usage. It does so by continuously identifying and eliminating unused memory in Java programs.

Conclusion

Let’s wrap up.

  • The CPU is the component responsible for executing programs we write.
  • The CPU only understands machine language/code, which is a sequence of zeros and ones.
  • Any program written in a high-level language like Java, C… must be translated into machine language for the CPU to run it.
  • A Java program can be executed on nearly any computer, thanks to the JVM.
  • A Java program is compiled for the JVM and then translated for the actual hardware.
  • The two most important benefits of the JVM are the possibility to write a code once and run everywhere, and automatic memory management.