JVM Architecture Explained

Introduction – What Is Java Virtual Machine In Java? 

Java Virtual Machine (JVM) is the virtual machine that runs the Java bytecodes. It is a software implementation of a computer that executes a computer program. JVM is platform-independent. That means a bytecode compiled on one platform can run on any other platform, provided it has a JVM. JVMs are available for many hardware and software platforms. While the number of JVM languages has grown recently, Java remains the most popular. Java is still used by over 90% of developers, indicating that it is a very popular language. 

Bytecodes are the machine codes for the JVM. When we compile a Java program, the compiler converts the source code into bytecodes. These bytecodes can be executed on any machine that has a JVM installed on it. The JVM interprets these bytecodes and executes them accordingly. 

The JVM has three main components: classloader, memory area, and execution engine. The classloader loads the class files into memory and makes them accessible to the Java application. Memory area consists of different parts like a heap, method area, stack, etc. where data is stored during the execution of a program. The execution engine contains the interpreter and just-in-time compiler that interprets and executes the bytecodes accordingly. 

How does Java Virtual Machine Works In Java? 

Java Virtual Machine is software that provides a platform to run Java programs. JVM converts the Java bytecode into machine code on the fly. When we compile a Java program, it is not converted into machine code that can directly run on the CPU. The compiled code is called bytecode, and JVM executes this bytecode. So, JVM is hardware and operating system independent. This means we can write a Java program on one computer and run the same program on any other computer, provided it has a JVM. 

JVM loads the class file and converts it into memory locations. Then, it verifies whether the bytecode is valid or not. After that, it executes the bytecode line by line and provides output. JVM also provides security by isolating java programs from each other and also from the operating system. This makes java more secure than other programming languages. Java programs run slower than natively compiled languages like C because of interpretation and verification overhead but in recent years, the Just-In-Time compilation has improved the performance of Java programs significantly. Therefore, JVM forms the heart of the Java programming language and provides platform independence to it.  

Below is a flow chart that explains how JVM works:  

                                                         |———> Verifies Class File  

                                                         |                                |—-> Executes Program  

       |——–> Loads Class File  <———|          |  

       |                                      |          |—-> Provides Output  

Java Program                                  |—-> Generates Bytecode  

       |  (Source Code)                        |   

       |                                      |          

       |–> Compiles Program                   |—-> Security   

                                                         |  

                                                         |–> portability    

As we can see from the JVM diagram, the Java source code is first compiled into bytecode by the compiler. Then this bytecode is fed to JVM, which loads it into memory, verifies it, executes it, and provides output. JVM also helps in making java programs more secure and portable across different platforms. Therefore, we can say that JVM forms the heart of java programming language, making it platform independent. 

Software Code Compilation & Execution process 

Java is a programming language compiled into bytecode, which runs on any computer with a Java Virtual Machine (JVM). The Java compiler translates the source code into bytecode, a platform-independent format. The JVM then executes the bytecode, translating it into instructions that the computer can understand. 

The whole process of compilation and execution can be summarized as follows: 

  • The programmer writes the Java code in a text editor. 
  • The code is saved in a .java file. 
  • The Java compiler translates the .java file into a .class file containing bytecode. 
  • The JVM executes the bytecode in the .class file, producing output.[Text Wrapping Break] 

Java code Compilation and Execution in Java VM 

The JVM architecture in Java uses a Just-In-Time (JIT) compiler to compile Java bytecode into native machine code on the fly. The JIT compiler is used for performance since it can optimize the code for the specific hardware it is running on. The JVM also has a garbage collector that runs in the background to reclaim memory from no longer-needed objects. The garbage collector runs as a low-priority thread, so it does not interfere with the program’s execution.  

The Java platform itself is comprised of three main components: the Java language, the Java virtual machine, and the Java API. The Java virtual machine is a platform-independent execution environment that converts Java bytecode into native machine code. The Java API is a library of pre-written code that provides a standard set of interfaces for common tasks such as file I/O and network communication. 

When a Java program is started, the JVM loads the class files that are needed to run the program. The class files contain bytecode, which is instructions that are compiled for the JVM. The JVM interpreter then executes the bytecode, which calls methods in the libraries that are part of the JVM. When a method is called, the JVM looks up the method in the class file and then executes the bytecode for that method. An error will occur if the method is not found in the class file. 

We can also embed Java VM in devices like routers and printers. In this case, the device will have its own operating system and will not need to install a Java runtime environment. The advantage of using a Java VM is that it makes it possible to write platform-independent code, which is code that can run on any device with a Java VM without change. 

Conclusion 

The Java Virtual Machine is a powerful tool that allows developers to write code that can be executed on any platform. By understanding the basics of the JVM architecture, developers can create code that is portable and efficient. While the JVM has many features, the most important for developers to understand are the class loader and bytecode interpreter. By understanding how these components work together, developers can create code that runs efficiently on any platform. 

Related Articles

} }
Request Callback