The Java Virtual Machine

The Java vision was to empower developers to “write once, run everywhere”. One way of achieving platform independence is to use middleware to mask differences. Enter the JVM; a stack-based virtual machine that uses 32-bit words, performs arithmetic using 2-complement and can execute compiled Java bytecode, typically .class or .jar files.

The beauty of middleware ensures that just about any bytecode can run on the platform; it doesn’t necessarily has to be only Java bytecode. Some popular languages that run on the JVM include Scala, Groovy, Clojure, PHP, Python, JavaScript, Erlang, Ruby and lots more. Security is facilitated by using a sandbox model which specifies what actions running code can execute on a machine. The components of the JVM include the stack, registers and method area.


Registers are simulated by the java program; some of them are:

Program Counter: Points to the location of the next instruction to be executed.

Optop: Points to the top of the operand stack; identifying the currently executing method.

Frame: Points to the stack frame of the currently executing method.

Vars: Points to the local variables of the currently executing method.

The Stack

This part is responsible for storing method arguments as well as local variables in any method. New method calls result in new stack frames being pushed on this stack. Once a method returns, its stack frames are popped off the stack. Stack frames have three sections: local variables, operand stack and frame data.

The Method Area

This is where the classes used by the currently executing method are stored. It tracks the current instruction and the program counter is set to the next instruction here too. Method area is also shared by all the threads of a process; for multi-threaded processes, monitors are used to ensure synchronization.

The Heap

This is where objects are stored; (if you have ever had to increase heap size for programs with large memory requirements; then this is the place). Whenever you want to create a new object; memory is allocated from this storage.

Given that the JVM only translates bytecode into actions and operating system calls; this is insufficient to run java programs. Typically, the JVM is distributed with standard class libraries that allow you to make calls to the underlying system, do I/O and related stuff. The Java Runtime Environment (JRE) consists of the JVM and Java API classes; the Java API classes are needed to make system calls and also provide the necessary API interfaces to the JVM.

SoYouKnow: The original Sun JVM was written in C++ :); moreover you can write your own JVM provided you follow the standards specified by Oracle.

So thats it! Hope you liked it; if you do please share with your friends.

3 thoughts on “The Java Virtual Machine

  1. Great piece mate! A light and smooth dive into the JVM is a must have for new developers. Would be great if you could mention the JIT(Just in time) though, a piece on JVM would def be worth mentioning JIT.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.