In this section we will discuss about the Java Virtual Machine and its components in detail.
- Platform independent feature of Java
- Java virtual Machine
- Byte Code
Platform independent feature of Java:
What is Platform. In simple word, Platform is the environment to act. In software world, it is the combination of hardware and software where every program gets executed. Some of the platform are Windows, Linux, Mac OS etc.
In previous section, we have understood the difference between C/C++ and Java. One biggest difference is Java is Platform independent whereas C/C++ are platform dependent. Let’s discuss how Java is platform independent in briefly.
As we know, Java is simple and robust Object-Oriented programming language. Credit for Platform independent feature of the java is always goes to JVM.
Abbreviation of JVM is Java Virtual Machine. Before going to the depth of Java virtual machine let’s understand what Virtual Machine is.
Virtual Machine is the abstract system of a single computer into several different virtual system to make the feel the different environment or platform implemented in the single system.
Each virtual machine are operated differently from each other and Only a particular operation is executed in a particular virtual machine. So running in virtual machine means not only running in different platform but also running application in different CPU.
Java Virtual Machine:
Now you are clear what is virtual machine. Let’s discuss some more point on Java Virtual machine and how it is working in java programming language.
JVM acts like a translator between high level language and the machine level in Java. As we know Java is high level programming language. When Java code is compiled then it will convert to the .class file. .class file generally is the byte code. JVM convert the byte code to machine level language and perform the action.
So you can run the .class file in different platform without having java source file. Only you need to install JDK in that platform. So that JVM will access the byte code and convert it to machine level language and perform the action based on the programming language. This makes the Java as platform independent programming language.
How JVM works?
Let’s discuss on briefly.
From the above graph you can notice that JVM has couple of components which perform different task to complete the JVM feature. Let’s discuss about each component in details.
Class Loader Subsystem:
From the name, you can guess the functionality of class loader subsystem. i.e it is something related to loading.
As discussed earlier, when java code is compiled then .class file is generated. And JVM implicitly call the class loader subsystem to load the .class file to the JVM.
That means the main functionality of class loader subsystem is Loading the class file to the JVM.
There are 2 types of loading available in JVM.
- A boot strap loader
- User defined class loader
Class loader loads the .class file to the JVM in certain steps as below.
- Loads class file
- Checks the correctness of class file
- Allocates the memory for the class file
- Transform the symbolic ref to direct ref
- Set the default value for the class variable for which it allocates memory.
From the above section, you notice that class loader subsystem allocates the memory for the class variable. What is class variable?
Class variable is basically known as static variable. We will discuss more about the class variable / static variable in upcoming section.
In which memory this class variable is allocated?
Yes, Method area is the location where class loader subsystem will allocate the memory for the class variables. The size of this method area is not fixed. It can be expanded or reduced based on the size of the application.
When objects are created through the new operator, memory is allocated from heap. So, in Heap location, Object and array is saved. There is another thread available in heap called Garbage collector which delete the object in heap whose reference is not active in stack.
Local variable and argument of methods are allocated in Java stack.
PC register is called as Program Counter Register. The main task of PC register is to track the sequence of running the application. It stored the address of the instruction to be executed next.
Native Method Stack:
Native method execution will use the Java stack as well as Native method stack. All the library of the Native method stack is available in Java Virtual Machine through Java Native Interface.
It executes the byte code generated by .class file It contains Interpreter and Just In` Time compiler.