Before you become a Java programmer, you must understand every aspect of this language. One of the most crucial aspects of Java is the Java architecture. Another technical topic in this regard is knowing the components of JVM. So, we’ll cover both of these critical topics in this write-up.
This article assumes that you have a general understanding of Java’s basics. Even if you aren’t familiar with Java’s fundamentals, you can ask any questions you might have in the comment section below. Let’s get started:
Java Architecture Explained
Java has two processes, compilation, and interpretation. The Java Compiler converts the code present in Java into byte codes. The Java Virtual Machine (JVM) converts the byte codes into machine code which the machine executes directly.
We can describe this process in the following steps:
- The source code goes to the Java Compiler
- The Java Compiler converts it into byte codes
- The byte codes go to the Java Virtual Machine
- Finally, it goes to the OS (operating system)
Now that we’re a little familiar with how the Java architecture works, we can take a look at its different components:
JVM (Java Virtual Machine)
The creators of Java wanted it to be WORA (Write Once Run Anywhere). This means you can run its applications on any platform, but the thing that gives Java this quality is JVM. The JVM provides the environment to execute Java code. It interprets the bytecode and converts that into machine code so the machine could run the Java program.
JVM loads verify and execute the code. It also provides the runtime environment to the code so it could run in the machine.
JRE (Java Runtime Environment)
The JRE builds a runtime environment where you can execute the Java programs. It takes the Java code and combines the same with the required libraries. The JRE also initiates the JVM for its execution. The Java Runtime Environment has the required software and libraries so you can run the programs.
JDK (Java Development Kit)
You’d use the JDK to develop Java apps and programs. It is a software development environment and contains multiple development tools including the JRE, a compiler, an interpreter, a documentation generator, and an archiver with others.
Components of JVM
Now that you’re familiar with the Java architecture let’s take a look at the components of JVM so we can understand it better. As the JVM performs one of the most crucial tasks for Java, it has many parts for that purpose. We’ll discuss each element in detail:
ClassLoader is a subsystem of the Java Virtual Machine which loads class files. It’s the first component of the architecture as it loads the program so other tasks can take place. It also links and initializes the class files. We can divide its working into the following three sections:
This component loads the classes. It has the BootStrap ClassLoader for loading classes belonging to the bootstrap classpath. The Extension ClassLoader loads classes situated inside the ext folder, and the Application ClassLoader loads path mentioned Environment Variable and similar files.
Here, the subsystem has a verifier to verify if the bytecode is correct or not. If the bytecode isn’t proper, it will generate the verification error. The linking section allocates all static variables memory and assigns the default values. It also replaces the symbolic references of memory with original ones.
In this section of ClassLoading, the system assigns the static variables to the original values and executes the static block.
Runtime Data Area
This section of the JVM has the following components:
The method area stores all the class-level data. Every JVM has only one method area.
The heap area stores all the Objects and their instance arrays and variables. Like the method area, a JVM has only one heap area.
This section creates unique runtime stacks for every threat and makes an entry for every method call in the stack memory (also known as Stack Frame). It has a Local Variable Array which is related to the method, the operand stack, which acts as a workspace for intermediate operations, and the frame data, where all symbols related to the method remain stored. The frame data maintains the catch block information unless there’s an exception.
Every thread has separate PC Registers which hold the address of the running instructions. Once an instruction has completed execution, the PC register updates itself with the next one.
Native Method Stacks
As the name suggests, this section holds information about the native method. It creates a unique native method stack for every threat.
You should note that the first two components of the Runtime Data Area (method area and heap area) are shared resources while the stack area is not.
The Execution Engine executes the bytecode. It reads and executes it piece by piece. It also has different components:
This component interprets the bytecode quickly but is a little slow in execution. It has a significant drawback as when the system calls one method multiple times, and it requires a new interpretation every time. This drawback of the interpreter damages the efficiency of the process substantially.
The JIT Compiler doesn’t have the drawback the interpreter has. So, when the Execution Engine finds repeated code, it uses the JIT Compiler instead of the interpreter. The JIT Compiler converts the bytecode into native code after compiling it. The system uses the native code directly.
The JIT Compiler has the intermediate code generator for producing intermediate code and the code optimizer for optimizing the same. It also has a target code generator that produces the native doe and a profile that finds hotspots.
The final component of the Execution Engine is the garbage collector which gathers and gets rid of unreferenced objects. You can trigger it by calling system.gc() however that doesn’t guarantee its execution.
Apart from these components the JVM also has the JNI (Java Native Interface) and the Native Method Libraries. The former interacts with the latter and provides the necessary Native Libraries for execution.
We’ve tried to keep this article concise and helpful, and we’re confident that you found this informative. If you have any questions about the components of JVM or the Java architecture, you can ask us the same by contacting us through comments.
You can learn more about Java and its applications on our blog. We have numerous helpful guides and articles that cover various aspects of this robust programming language.
If you’re interested to learn more about Java, OOPs & full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.