Java Virtual Machine - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts


You know that Java codes run in Java Virtual Machine (JVM), which is known as the heart of Java Technology. It is an abstract computer. Now you have to understand, What is really going on there?

The name of any Java source file has .java suffix, just like .c extension for C source files. Suppose for example our Java program source code is all in one file, X.java, and that for instance, we are doing our work on a PC running Linux.

We first compile the code using the Java compiler javac as shown below:

javac -g X.java

(The -g option saves the symbol table for use by a debugger.)

It produces the executable Java file, X.class , known as byte code, to run on a Java Machine . But we don't have such a machine.

Actually, JVM runs the byte code. Instead, a program emulates the operation of such a machine. That is the reason for using the term "virtual" . That emulator program is named Java Virtual Machine.

We then run our program:

java X

Note that Java program not only runs on a virtual machine , it also is in some sense running under a virtual operating system . For example, you can compare printf() function in C with System.out.println() in Java . If our real machine is running Unix , a printf() call in a C program calls the write() function in the OS. But this does not happen in Java. Java program is running on the JVM which runs on Unix OS . Thus, System.out.println() a call is made in the JVM, and later the call is made for write() on the real machine.

When you write and run a Java program, first the source files are compiled into Java class files. The class files run on a Java Virtual Machine. When you write code to access system I/O resources by calling methods in your classes, it uses Java Application Programming Interfaces or Java APIs. When your program runs, Java APIs are called as the I/O methods are invoked.

The following Figure depicts how Java programs are compiled and executed.

Java Virtual Machine is implemented as a software module on top of a host operating system. A Java method is compiled to bytecodes, and stored in class files. Whereas native methods are written in other languages, such as C, C++, or assembly, and compiled to the native machine code for a particular processor and these are stored in a dynamically linked library. At the time of running Java programs, native methods are called and dynamic libraries (containing native methods) are loaded by JVM.

A system, where bytecodes are executed in silicon (on a "Java chip"), must be implemented as a Java-based operating system. Thus to run these bytecodes on any type of operating system, the virtual machine is implemented as a software module on top of the host operating system. The Java API access the host resources through native methods. It makes Java programs platform-independent.

JIT Compiler

In JRockit JVM, the first step in the machine-code generation process is known as just-in-time (JIT) compilation.

JIT converts the byte code into native machine code before a method executes, but JRockit does not have an interpreter. This conversion is performed when a method is called first time. JIT is first enough and generates efficient code to run quickly. Moreover, frequently called methods are further optimized.

As it is compiled code, not interpreted, the generated code is significantly faster.



Happy Exploring!

No comments:

Post a Comment