dcsimg

What is the Java Virtual Machine (JVM)

If you are new to the world of programming or developer apps in a language other than Java, you may have come across the term JVM before and wondered what it stood for. We look at the term JVM as it relates to Java developers and discuss what, exactly, it does, how it functions, and why it isn't just about Java.

JVM stands for Java Virtual Machine and is a program that was designed to run - or execute - other programs. Most people assume the JVM only works on programs created solely in Java, but that simply is not the case. Or, rather, it goes a little deeper than that.

Released back in 1995, the JVM had two main functions: to make it so that Java applications could run on any computer, regardless of its operating system or OS architecture, and to optimize program memory for optimal performance. The whole philosophy behind the JVM operates on the principle of "Write once, run anywhere," meaning you do not have to re-write your code for each platform you want it to run on.

Prior to the invention of JVM, that was not the case. Developers at that time had to write software for specific architectures, and any program memory management was left up to the programmer.

Java Virtual Machine Explained

Java Virtual Machine Explained

The basic definition of the JVM is that it is an application that runs code and supplies the runtime environment for your software's code. However, when developers talk about the Java Virtual Machine, they tend to speak about it in more general terms, such as "The JVM runs my software. Period."

Of course, the JVM is much more complicated than that, even if it could - sort of - be true. In reality, there are several key parts of the Java Virtual Machine, including memory management, garbage collection, the Java execution engine, and the Java class loader.

As the name implies, the JVM is a virtual machine. That means that it is an abstraction of a real machine, which could be the computer or server that your software is being executed on. Because the JVM does not actually make its own virtual OS, however, there is a debate about whether it is truly a virtual machine. You may also hear it referred to as a managed runtime environment because of this. Another name for the type of software JVM represents could be a process virtual machine.

Whatever type of application you call it, the JVM creates its own environment on the end-users machine in which to run its applications. JVM is also a specification, which we will delve into in the next part.

What is the JVM Specification

The JVM is made up of three parts:

Specification: The JVM specification is a software specification. This specification is interesting because its implementation details are not defined. That may sound strange, but the idea is that Java developers should be allowed a large amount of creativity. The specification basically states that the JVM must execute Java software properly. That leaves a lot of leeway for interpretation.

Implementation: The implementation of the JVM is known as the Java Runtime Environment (JRE).

Instance or Runtime Instance: You create an instance of the Java Virtual Machine when you run the Java class through the command prompt. Another way to view it is that once the JVM specification has been implemented and you release it as a piece of software or an application, it is able to be downloaded and run on a user's program. The download version of your program is an instance of the JVM. This is also known as an instantiated version or instantiation of the JVM.What is the JVM Architecture

The JVM is made up of eight key components. They are the Classloader, Class Area, Heap, Stack, PC Register, Native Method Stack, Execution Engine, and Native Method Interface.

The classloader is used to load class files. At runtime, when we execute the Java program, it gets loaded by the classloader. Java has three built-in classloaders: Bootstrap ClassLoader, Extension ClassLoader, and System and Application ClassLoader.

The Class Area, better defined as Class(Method) Area, is used to store class structures, including the runtime constant pool, field data, method data, and method code.

Objects get allocated in the runtime data area known as the Heap.

Frames hold local variables and play a role in method invocation and method return. They are stored in the Stack. Every thread has its own private JVM stack, which gets created in the same moment as the thread. Each time a method is invoked, a new frame is created. Those frames get destroyed when their method invocation resolves.

The program counter register holds the address of the JVM instruction currently getting executed.

The Native Method Stack holds all of the native methods used in the application.

The execution engine contains three items: a virtual processor, an Interpreter that executes bytecode stream instructions, and the JIT (Just-in-Time) compiler.

The Java Native Interface - or JNI - is a framework that provides a communication interface with other programs written in a different language, such as C or C++ (to name but a few). The framework is also used to send output to the Console and interact with Operating System libraries.

JVM Languages

There are a number of computer programming languages that can run on the Java Virtual Machine other than Java. The following is a quick breakdown of some of the more popular ones.

Kotlin: Kotlin is a general-purpose, Open Source programming language. It is statically typed. It was created by the team at JetBrains, who are known for their developer tools and IDEs. Kotlin is interoperable with Java and completely supported by the Android platform, making it a great choice for programming mobile apps.

Scala: Scala - or scalable language - is another statically typed developer language. It is also an Object-Oriented programming (OOP) language with a focus on functional programming. It got its start back in 2004 and has seen a steady rise in popularity over the years. Scala features algebraic data types, pattern matching and Regex, and multithreading, to name a few.

Clojure: Clojure runs on the Java Virtual Machine and also Microsoft's Common Language Runtime (CLR). Is it a functional programming language and a general-purpose language. It is technically an offshoot of Lisp, with the capability of running on the JVM.

Groovy: Groovy is an Object-Oriented, optionally typed programming language. It boasts a simple syntax for low learning curves. It was designed to integrate with Java (interoperability) and adds some interesting features to Java, including scripting and operator overloading.

JRuby: JRuby is basically Ruby modified to run on the JVM. It is a hybrid Object-Oriented programming language that mingles Ruby and Java libraries.

Jython: Jython, similar to JRuby, is the Java implementation of Python, designed to run on the JVM. Jython is an OOP language that has multithreading and uses Java's garbage collector and classes from Java Libraries. It also hosts many of Python's modules, which makes it a powerful JVM programming language.