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.
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
Parses bytecode line by line.
The JVM pushes and pops values from a stack based on the parsed line of bytecode.
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
Class loader to load all
.java
files into.class
bytecode files for the JVM.Execution engine, which consists of the interpreter and JIT, to actually execute the bytecode instructions.
Methods area, a place where information about the loaded classes like methods or fields is stored.
Heap, to store objects and arrays of the program.
Stacks to keep track of method frames and local variables.
Process of Running the Program
The class loader loads all the files to
.class
type for the JVM.The bytecode verifier then verifies the bytecode for correctness.
The execution engine starts executing the code using the interpreter or JIT compiler.
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.