Java Virtual Machine is a very great architecture that helps Java to become Platform Independent Programming Language.
We are going to look further in-depth about JVM. Keep Reading
1. What is Java Virtual Machine?
Basically, Java Virtual Machine is a runtime engine of your Java Application and it is a part of JRE ( Java Runtime Environment ).
If you’re quite familiar with any other programming language then you’ve must hear of Main Method, that’s what JVM does. JVM calls the main method of your Java application.
The Java application is called WORA (Write Once Run Anywhere) applications that means you can write your code on a machine/OS and then can run of any other machine/OS without changing a single line of code or configuration.
As there is no need to understand JVM so deeply because millions of Java applications have already been developed without a developer understanding of JVM, but if you do so then you will understand Java more and that’s why we are here.
1. 2. What is a Virtual Machine?
As the word Virtual defines, Virtual is something that is not present physically.
Therefore, Virtual Machine is computer software that behaves like a physical device or software implementation of a physical machine.
To make Java platform-independent, running it on a virtual machine is very important and that’s why JVM was introduced.
Back to the topic, JVM analyzes and executes the Java bytecode that was compiled by the Java compiler.
As per the documentation, JVM doesn’t know the Java code but it only understands the class file and the bytecode available in a class file.
1.3 What is ByteCode?
As we discussed above, Java Virtual Machine uses bytecode and then convert it into machine code.
Therefore, ByteCode is a middle-level language between Java code and the machine language.
Only a Java Virtual Machine can understand the bytecode and even if the operating system is different bytecode will execute on the JVM because bytecode has no platform-dependent code and JVM need hardware to be run and bytecode needs JVM.
In this way, when Java code is compiled it converts to the bytecode and then bytecode is converted to the machine language with the help of Java Virtual Machine on any operating system.
2. JVM Responsibilities:
There are certain responsibilities of JVM in the JRE environment.
- Calling the main method of your Java application.
- Memory Management with automatic Garbage Collector.
- Loading and executing class files.
While loading the class file, the Classloader is responsible for loading dynamically at runtime.
2.1 What is ClassLoader?
Recommended Read: Classes in Java
The class loader is responsible for loading class files dynamically at runtime in the JVM.
Also, the class loader is smart enough that it loads the Java classes only when they are needed by the program.
This means that not all Java classes are loaded in the JVM at once.
Mainly, there are three types of classLoader. Let’s discuss them in detail:
1. Bootstrap ClassLoader
Do you know? class loaders are classes themselves. Then, who is responsible for loading CLASSLOADER?
So, now bootstrap comes into the picture or primordial classloader. This classloader serves as a parent classloader for all other classloaders.
- If every classloader(which is also a class) loads every other Java class then it may lead to recursion that’s why bootstrap is written in some special native code.
- Classes are loaded from rt.jar
2. Extension ClassLoader
This is just a child of the bootstrap class loader and it takes care of loading Java classes found in the $JAVA_HOME/lib/ext directory.
3. System ClassLoader
The system class loader or application class loader takes care of loading all the Java class in the JVM and it loads all the class from the classpath. It is a child of Extension class loader.
2.2 How ClassLoader Works?
Above mentioned classloaders are part of the JRE (Java Runtime Environment). Firstly, when the JVM request the call, classloaders comes into the picture and tries to find and load the class.
To load classes at runtime, the method that is responsible to do so is java.lang.ClassLoader.loadClass().
If a class is not loaded from parent class loader then it will recursively call child class loader.
In the end, if no classloader is able to load and execute the class then the following exception will occur.
java.lang.ClassNotFoundException: com.masterincoding.classloader.SampleTest at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:348)
3. Memory Management In JVM
One of the very important responsibilities of Java Virtual Machine is memory management where Garbage Collector comes into the picture. Let’s talk about facts before Garbage Collector and after Garbage Collector.
Before Garbage Collector, all programs and their memory management were done by the programmer which includes reclaiming the occupied memory that was used by an unused/inactive java program.
After Garbage Collector, this is the responsibility of Java Virtual Machine to manage memory by the automatic garbage collector, which identifies the unused memory by the program automatically.
Hence, it eliminates the unused memory by itself.
I know memory management is a very vast topic so that’s why we’ll be covering that in another section.