Friday, August 3, 2012

What is the purpose of JVM (Java Virtual Machine)?


JVM stands for Java Virtual Machine. It acts as an intermediate layer between the Java program and the actual hardware. A ‘virtual’ machine in which the Java code is executed or runs the compiled Java code. The Java code is compiled and converted to byte code by the compiler. The JVM understands the compiled code and executes the java byte code. 


The main reason JVM was developed was for code portability thus providing platform independence. Let us understand portability – take the below HelloWorld.java, the program is first converted to byte codes by the compiler. These byte codes need to be understood by the machine to execute it. If JVM is present in the machine, it understands the byte code, takes care of memory management and interaction with the actual hardware. JVMs are developed for different hardware and software platforms. All that the user has to make sure is that the machine contains a JVM, irrespective of the platform where the code will run.


Below is an example and flow of compilation and execution.

Here is a sample code for HelloWorld.java

public class HelloWorld {
 
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
 
}

Let us see the steps in the compilation and execution of this java program.


No comments:

Post a Comment