Java Virtual Machine

Since I am starting out with a technical blog, it’s reasonable to take off from the heart of Java, the JVM. With this small discussion, let me try to throw light into the following questions:

  1. How is Java Platform Independent?
  2. What is JVM?
  3. Is JVM Platform Independent?
  4. How is JVM Implemented?

Before we actually get onto the questions, it’d be good to have an overview of how the java language works.

The image above will give a rough idea on how the java code you write gets translated to an application. As a skeleton, the code you write goes through two stages before it gets translated into an application. Stage 1: The first stage is familiar with everyone. It is nothing but the compiling of the .java file you have with you. The javac compiler translates the Java code in the .java file to bytecode present in the .class file. The bytecode is nothing but an assembly language, similar to the 8085 machine language. But unlike the 8085 assembly language, our bytecode is not platform dependent. For bytecode, JVM is the platform. Stage 2: The bytecode in the .class file is then received by the Java Virtual Machine, which plays the role of converting it to the machine language recognized by the underlying hardware of a particular machine.

This is roughly how the java code takes its journey from the .java file to be an application.

Now, let us see the answers to the questions we have above:

1) How is Java Platform Independent?

One of the many reasons why java is overwhelmingly popular is that it is platform-independent. Be it Windows, Linux, Mac or even your blackberry..wham.. you have a java app. Well, what brings forth this magic? In a single word, it can be answered as the JVM. Most programming languages compile the code into machine language, which is suitable to be executed in specific microprocessor architecture. But java programs are not specific to a microprocessor. But it is run the JVM. The JVM acts as an emulator for the processor. As discussed above the JVM is responsible for handling the bytecode. Based on the bytecode, it will throw actions or system calls to the machine. 2) What is JVM? JVM or Java Virtual machine is in fact, the heart of java programming language. The JVM does the task of making the java programming language platform-independent. As explained above, the JVM roughly does the following steps:
Step 1: Take in the .class file.
Step 2: Create the action and system calls that manage the underlying hardware.
Step 3: The application runs using these actions and calls.

As seen in the image, we have two platforms, one is the Intel architecture, which has a Windows XP Pro Edition running, and also an AMD Platform which has a Red-Hat Linux running on it. The bytecode is in the .class format and is same for both the platforms. The .class file is received by the JVM which generates action calls to the hardware on which it is implemented. The curious thing to be noted here is that, even though JVM is present in Windows XP ans Fedora Linux, they are not the same copies, but different versions designed in such a way to work in separate platforms. More into this in the next section.

3) Is JVM Platform Independent? To make a long answer short, JVM is not platform-independent. It is as platform-dependent as it can be. As seen in the above figure, a JVM runs in a specific operating system, be it a C# one like Windows or a java based one. To run in a java based platform jvm has to be platform-based too. SUN( or ORACLE) claims there are over 4.5 billion JVM enabled devices in the world. The image shows the situation where a java enabled blackberry phone stopped responding due to java error.

5) How is JVM Implemented? We have seen JVM is the heart of Java and JVM is enabled in over 4.5 billion machines. The one question that deserves to be answered is how JVM is implemented. The answer is simple, through the Java Runtime Environment, or JRE. SUN provides JRE for each platform. The virtual machine, and it’s supporting code is defined as the runtime environment.