An Introduction to the Java Virtual Machine (JVM)

The Java Virtual Machine (JVM)

Java belongs to a family of languages called Compiled Languages . Any code written in such a language needs to be converted (compiled) to an intermediate form that can then be understood by the host platform (the OS/platform in which the code runs).

For Java, this intermediate form is called Bytecode which is then interpreted by a runtime called a Java Virtual Machine (JVM). Think of JVM as a piece of software that does the hard work of running your Java code. It takes care of memory allocation, thread management, garbage collection and so much more. Apart from Java, it also supports (read: able to run) code written in languages such as Groovy, Scala etc.

In Java, code is written and saved as .java files. The compiler (javac) operates on the java files and generates the equivalent Bytecode ( .class ) files. The java command would now be able to execute the Bytecode stored in the .class files. More on this later.

Java has become such a versatile programming language because of the JVM. The JVM is aware of the specific instruction lengths and other particularities of the underlying hardware platform. Because of this knowledge, the JVM is able to run any bytecode passed to it, eliminating the need for a programmer to rewrite or recompile code for each platform. This impressively versatile runability is what has led to Java being so prevalent in the computing world.

Just to add a little to the discussion, and perhaps add a little crumb of insight. Java is designed to “Write once, run anywhere” (WORA), or sometimes write once, run everywhere (WORE). ( Full Article )

So, the idea is to write your Java program and compile it to the “Platform Independant” bytecode. This bytecode is the same for all users regardless of what you will be running it on. The other key detail to understanding how this works is you will need to download and install a specific Java Virtual Machine for your enviroment, one flavor for Windows ( and perhaps more than one flavor - ie Win 32 and Win 64 ), another JVM for OSX, another for Linux, etc…

With these pieces in place your Java program compiled to bytecode can now be properly run on your platform via the interpreter provided by the specific JVM. In this sense Java is also an interpreted language, the bytecode gets interpreted at runtime. To make it a little more complicated, parts of the bytecode can also get platform dependant compiled by the JIT compiler; this is to optimize specific pieces of the program for performance.

Microsoft’s .NET serves the same purpose as the Java JVM. It allows languages like C# and VB ( and others ) to be WORA.

Java Virtual Machine (JVM) is a specification that provides runtime environment in which java bytecode can be executed. As the name implies, the JVM acts as a “virtual” machine or processor. Java’s platform independence consists mostly of its Java Virtual Machine (JVM) . JVM makes this possible because it is aware of the specific instruction lengths and other particularities of the platform. The JVM performs following operation:

  • Loads code
  • Verifies code
  • Executes code

In most cases, other programming languages, the compiler produce code for a particular Operating System but the Java compiler produce Bytecode only for a Java Virtual Machine . When you run a Java program, it runs as a thread within the JVM process. It is the JVM’s responsibility to load your class files, verify code, interpret them and execute them. When you issue a command like java , the JVM loads the class definition for that particular class and calls the main method of that class.

More about…Java Virtual Machine