Java Virtual Machine or JVM is a virtual runtime environment in which java bytecode can be executed. The JVM loads, verifies and executes Java bytecode.
Understanding How Code is Executed
Each type of processor has its own machine language, and it can execute a program only if that program is expressed in that language.
Although it is theoretically possible to write a program using machine language, it is practically unfeasible and error-prone even for the simplest programs.
In practice, therefore, almost all programs are written in languages adapted to people. Such programming languages are called high-level languages. Examples of high-level languages are Java, C, C ++, C #, Pascal, and many more.
Programs written in a high-level language cannot be executed directly on a computer but must first be translated into a machine language of a particular processor. This translation is done by special computer programs called compilers.
The compiler, therefore, receives a high-level program at the entrance and translates it into an equivalent machine-executable program. This machine language program can then be executed any number of times.
For the program to run on computers with a different type of processor, the program must be re-compiled into the appropriate machine language using another compiler.
An alternative to translating high-level programs into machine language for program execution is a process called interpretation.
Besides the compiler, which compiles the entire program, an interpreter is used simultaneously to translate (compile) and execute the program gradually command-by-command, as required by the logic of the program.
An interpreter is a program that works similarly to the processor in the “take command and execute” cycle.
More precisely, for the execution of a program, the interpreter takes one program command at a high level, determines the necessary machine operations for its execution, and finally performs them.
Note that if some command does not execute according to the logic of the program, it will never be translated.
On the other hand, if one block of code is executed more than once, it will also be interpreted multiple times.
This is why using this kind of interpretation is usually slower than executing the same program that was previously translated into machine language.
Java Code Execution and the use of Java Virtual Machine (JVM)
In the case of the Java programming language, an approach is used which consists of a combination of compilation and interpretation of the code.
As can be seen from the picture above, programs written in Java are still compiled to machine language. Still, that machine language is not a real processor’s language, but a fictional computer called a Java Virtual Machine (JVM). The machine language of a Java virtual machine is called a Java bytecode. The Java program is therefore compiled into a JVM machine language that cannot be executed directly on a real computer.
To execute a Java program translated into Java bytecode, you need a Java bytecode interpreter on the computer, which comes together with JVM.
Different Java bytecode interpreters are required for each computer type (different versions of JVM), but only one translation of a Java program into a Java bytecode is sufficient.
This is one of the main features of Java compared to other languages: the same translated/compiled Java program can be executed on different types of computers.
You need to install the appropriate version of JRE on your computer to execute Java bytecode.
Class Loader : Loads the Class for execution.
Method area: Stores pre-class structure as a constant pool.
Stack: Local variables and partial results are stored here. Each thread has a private JVM stack created when the thread is created.
Heap: Heap is a memory area in which objects are allocated.
PC Registers: Holds the address of JVM instruction currently being executed.
Method stack: It contains all native methods used in the application.
Executive Engine: Controls the execution of instructions. It contains:
- Virtual CPU
- Interpreter: Reads and executes Java bytecode
- Just-In-Time(JIT) compiler:
The JIT compiler is activated at run-time, not before code execution. So only the code that is currently executing will be compiled by the JIT compiler, which is why it is called the Just in Time compiler. Once the JIT compiles the code, that version is stored in memory and later used as needed rather than being recompiled. This significantly increased the performance.
Native Method Interface: Gives an interface between java code and native code during execution.
Native Method Libraries: Native Libraries consist of files required to execute native code.