What is JVM? Is this existing real?
Most of the java developers know how to program in java, but don’t know what exactly going with the inner side of the java application running. So in this article, there will be an in-detail description of JVM and functionalities.
JVM is not existing in reality and instead of having it in reality, this is used to execute the java bytecode. The JVM consists of three parts,
1. Specification-This is used to specify where the virtual machine is specified.
2. Implementation -This is described as Java Runtime Environment and it is responsible for installing the requirements needed to run the java application on each operating system.
3. Java Runtime Instance-This is used to create the instance in the JVM, whenever the running the java class.
The java was built using the concept called Write Once Run Anywhere, which means that it is running on a virtual machine. The compiler compiles the java file into .class file and that class file is input to the JVM.
The JVM is responsible for generating byte code for all kinds of platforms rather than different machine codes for each type of operating system. JVM executes the byte code generated by the compiler and produces output. JVM is the one that makes the java platform independent. And also I should mention that each operating system processes a different JVM and the output they produce after execution of byte code is the same in all the operating systems.
In interviews, people might ask how to run the java program in your local machine?. So in order to answer this question, let's assume we have a class Main.java and we need to run this class.
javac Main.java
This will read source files that contain module, package, type declarations written in the Java programming language, and compiles them into class files that run on the Java Virtual Machine.
java Main.java
Then run the program using the above command.
JVM Architecture.
So in the above, we have discussed what is JVM and also how to run a java program. So in here, we will have a look at how the JVM architecture works.
The main task of the JVM is to load the data, store them in the memory area and execute them after.
The class loader reads the .class file and saves the byte code in the method area. Then this will go into the runtime area, and it has,
- Method Area
- Heap
- Stack
- PC Register
- Native Method Stack
In here the method area and heap are per virtual machine which is shared among all the classes which is responsible for keep class level information and objects are allocated.
Others are,
Stack — Store the temporary variable.
PC Registers — Keep track of which instruction has been executed and which one is going to be executed.
Native method stack — access the runtime data areas of the virtual machine.
Just-in-time compiler is the part of the Java Virtual Machine (JVM). It interprets part of the Byte Code that has similar functionality at the same time.
Why java is slow?
Java slow, because of the following two reasons.
1. Dynamic Linking — The linking is done at the runtime and everytime the java program runs, the linking also need to be done.
2. Runtime-interpreter — The conversion of the java byte code in to native machine code is done at the runtime of the java program.
What is JDK?
The JDK stands for Java Development Kit. This contains tools and libraries which is necessary to develop software applications. The main purpose of JDK is to convert the source code into a fomart that the Java Runtime Environment (JRE) can execute.
JDK includes the java runtime environment (JRE), an interpreter, a compiler (javac), a achiever (jar), a document generator (javadoc) and some other development tools. If you want only to run the applications on your machine, then you do not want to have the JDK and you can only rely on the JRE.
So JDK is a software package you download in order to create Java-based applications.
Why java is platform independant?
Java is know to be platform independant. But why? This is because of the java can be run on any operating system. Let’s have a quick look into this.
Most of the other programming languages are compile directly into executables or binaries that is only capable of running on specific operating system. So in java, it creates a bytecode, which has high-level set of instructions that are general enough not to be platform-specific. Every operating system has a specifically design JVM, inorder to run the java programs.
So in this article, I described the basics of java programming which every java developer should know before creating a java application. Because, not having a proper foundation, will not be able to succeed in your career as always. Hope this tutorial is worth your time.