The Java Virtual Machine

Learn about the Java Virtual Machine

The Java Virtual Machine

The Java Virtual Machine, or JVM, is a machine on devices that provides devs a Java runtime to execute bytecode.

Bytecode

Bytecode is a set of instructions executed by a software interpreter. They are usually compact numeric codes, constants, and references, which are encoded results of the program's source code.

Bytecode

Representation of bytecode

Bytecode is designed to be run by an interpreter, not a hardware processor. Since the same bytecode can be run on different devices, programs compiled to bytecode are platform-independent, which means they can be run on different operating systems and platforms with the same output. For Python, bytecode files end with .pyc, and for Java, they end with .class.

Advantages of Bytecode

  • Platform independence — The same bytecode can be run on different devices, so a program compiled to bytecode is platform-independent.

  • Size Efficient — Bytecode can make things faster because an interpreter is used to read the bytecode, which requires it (the bytecode) to be compact.

  • Size — Bytecode can be compiled with a JIT (just-in-time) compiler (comes with the JVM) for better performance.

Process of a Bytecode Interpreter

  1. Parses bytecode line by line.

  2. The JVM pushes and pops values from a stack based on the parsed line of bytecode.

  3. Local variables are created and altered based on the instructions.

The process of compiling and running Java code

Key Points about the JVM

The JVM specifies how a Java program executes. It can be found in many places like Oracle, OpenJDK, etc (for download). As I mentioned earlier, the bytecode run by the JVM is executable on many different platforms, providing a nice environment for developers. The JVM also manages memory and garbage collection while running a program. To ensure the integrity and security of the bytecode, the JVM verifies it using some method to make sure nothing's wrong. The JVM comes with an interpreter for bytecode and a JIT (just-in-time) compiler to increase performance.

Main Components of the JVM

  1. Class loader to load all .java files into .class bytecode files for the JVM.

  2. Execution engine, which consists of the interpreter and JIT, to actually execute the bytecode instructions.

  3. Methods area, a place where information about the loaded classes like methods or fields is stored.

  4. Heap, to store objects and arrays of the program.

  5. Stacks to keep track of method frames and local variables.

Process of Running the Program

  1. The class loader loads all the files to .class type for the JVM.

  2. The bytecode verifier then verifies the bytecode for correctness.

  3. The execution engine starts executing the code using the interpreter or JIT compiler.

  4. While the program is running, the JVM manages memory and garbage collection.

Conclusion

In this article, you learned about the JVM and how it executes Java programs. There are other virtual machines for Python and other languages. It's not only for Java. In the next article, I will take you through how to install the JDK and Eclipse, and IDE for developing in Java.