JVM | Detail Description
The one, who is learning Java language, should know the complete process of JVM, that what are the requirements such as compiler, how to compile, how to execute, what are the steps of execution, etc. to understand and take the feel of the language.
As we have earlier discussed the definition of JVM, we further move to understand the things following by the briefing the JVM once again.
JVM (Java Virtual Machine)
JVM is the abbreviation of Java Virtual Machine which is considered as the heart of Java programming language. It is also an abstract machine. It is a specification that provides the runtime environment in which Java byte code can be executed. Its implementation is known as JRE (Java Runtime environment). An instance of JVM is created, whenever you write Java command on the command prompt to run the class.
Ability of Java Virtual Machine
Java Source code
- Java source code essentially consists of a main() method.
- The main() method is public and thus can be called by any object. It is also static and so it can be called without instantiating the object of the class.
- The main() method does not return any value.
- The controlling class of every Java program or application usually contains a main() method. This can be avoided to allow the class to be tested in a stand-alone mode.
- The other methods can subsequently be called in main() method.
Execution on JVM
- JVM executes “Java byte codes”, instead of executing source code directly.
- The codes of other programming language can also be executed by JVM, if codes are converted to adequate Java byte codes.
- Java Virtual Machine is different for different platforms and can also act as a platform itself.
- JVM also supports the automatic error handling by intercepting the errors which can be controlled easily.
- The above feature is useful in platform independency and multi-user ability of Java.
Compilation of Java source code
- To compile the source code, compiler requires knowing the type of every class used in the program source code.
- This can be done by setting a default user environment variable named as classpath.
The Java Compiler (Javac) reads the source code and converts it into byte code files known as class files.
Basically JVM has two main threads, which are as following.
The Demon threads are being run by JVM for itself, and JVM only decides on a thread for being a demon thread. It is also used for garbage collection.
Program’s main() is the initial and non-demon thread. The other implementations of threads are also known as non-demon threads. As far as any non-demon thread is active, Java Virtual Machine is also active.
Architecture of JVM
Now let’s start understanding each term one by one.
Class loader: It is a subsystem of Java Virtual Machine that is used to accept and load files.
Compilation creates class files.
Class or Method area: It stores per-class structure such as runtime constant pool, field and method data, the codes for methods, etc.
Interim memory is required during execution. It consists of heap, stack, registers to store data, etc.
Heap: It is the runtime data area in which objects are allocated.
Stack: Java stack stores frames. It also holds local variables and partial results, and plays a role in method invocation and return. Each thread has a separate and private Java stack that is created at the same time as thread. Whenever a new method is invoked, a new frame is created, and when the invocation completes the frame is destroyed.
Java native methods and libraries: It contains all the native methods and libraries used in the application.
Program counter (PC) register: It contains the address of the JVM instruction currently being executed.
Execution engine: It is the support of JRE or runtime environment and contains the following.
- Interpreter that reads byte code stream and then execute the instructions.
- A virtual processor i.e. JVM (Java Virtual Machine)
- JIT (Just-In-Time) compiler which improves the performance by reducing the processing time.
Java Virtual Machine runs two main threads i.e. demon and non-demon.
Thank you for your patience 🙂