What is JVM?
JVM stands for Java Virtual Machine, which is a crucial component of the Java Platform. It is an execution environment for Java programs, allowing them to run on different operating systems without the need for recompiling the source code for each individual operating system.
The Java Virtual Machine also ensures compatibility with previous versions of Java, meaning that Java programs from older versions can still run on newer Java versions without modification. This ensures the stability and continuity of Java applications when there are new updates to the JVM and Java.
What are the tasks of JVM?
1. Loading Java bytecode and class files of the Java application.
2. Verifying the validity of bytecode and class files.
3. Executing bytecode and class files using either an interpreter or a Just-In-Time (JIT) compiler.
4. Providing the runtime environment (JRE) for Java applications.
5. Managing memory and objects of Java applications, including allocation and garbage collection.
6. Providing utility functions such as error management and logging.
7. Offering standard APIs and libraries for Java application development.
8. Ensuring the security of Java applications by controlling access permissions and providing a controlled execution environment.
9. Enabling communication with native libraries and applications written in other languages such as C or C++ through the Java Native Interface (JNI).
10. Providing definitions for areas such as memory regions, class file formats, registers, garbage-collected heap memory, and reporting severe errors.
What is the JVM's architecture?
The JVM Architecture (JVM Architecture) includes the following components:
JVM structure in Java
A Classloader is a subsystem of the JVM (Java Virtual Machine) used to load class files and resources of a Java application. When we run a Java program, it is loaded by a classloader. There are three built-in classloaders in Java.
Bootstrap ClassLoader: This is the first classloader and is the parent classloader of the Extension classloader. It loads the rt.jar file, which contains all the class files of Java Standard Edition, such as the java.lang, java.net, java.util, java.io, java.sql classes, and so on.
Extension ClassLoader: This is a child classloader of the Bootstrap classloader and the parent classloader of the System classloader. It is used to load .jar files that contain Java extensions and third-party APIs.
System/Application ClassLoader: This is a child classloader of the Extension classloader. It loads class files from the classpath, which can be an environment variable or a command-line option that allows users to specify the location of class files used in the Java application.
Class (Method) Area
The Class (Method) Area is used to store metadata of Java classes and program methods.
This is the storage area for Java objects allocated by the program. When a Java object is created, it is stored in the heap. When the object is no longer in use, it is removed from the heap by the garbage collector and the memory space is released.
The Java Stack stores frames. It holds local variables and partial results and plays a role in method invocation and return.
Each thread has its own JVM stack, created simultaneously with the thread.
A new frame is created each time a method is called. The frame is destroyed when its method invocation completes.
Program Counter Register
The PC Register holds the address of the currently executing instruction from the Java virtual machine. When a Java program is executed on the JVM, the PC Register stores the address of the next instruction that the program will execute. When the current instruction is executed, the value of the PC Register is updated to point to the address of the next instruction in the program.
Native Method Stack
It contains all the native methods used in the application.
It consists of:
A virtual processor
Interpreter: Reads bytecode instructions and executes them.
Just-In-Time (JIT) Compiler: Used to improve performance. JIT compiles portions of bytecode with similar functionality at the same time, reducing the time needed for compilation. Here, the term "compiler" refers to a translator from the instruction set of the Java virtual machine (JVM) to the instruction set of a specific CPU.
Java Native Interface
Java Native Interface (JNI) is a framework that provides an interface for communicating with another application written in a different language such as C, C++, Assembly. Java uses the JNI framework to send output to the console or interact with operating system libraries.
How JVM Works in Java
The Java Virtual Machine (JVM) reads and executes bytecode generated from Java source code after it is compiled. When a Java program is compiled, it produces a bytecode file that contains encoded byte-level instructions. The JVM takes this file and translates it into machine code for execution on the system.
What is JVM Loading?
With JVM, you don't install it independently. When you download and install JRE or JDK, the JVM is also downloaded and installed simultaneously. The choice between downloading JRE or JDK depends on your needs.
If you only want to run Java applications without developing or modifying source code, you can download and install JRE to use the JVM.
If you want to develop Java applications or use development tools such as compilers and debuggers, you need to download and install JDK.