Witscale Test Center

A.1 How Java works > A.1.2 How Java program runs?


A.1.2 How Java program runs?

The Java Virtual Machine actually executes instructions from the class files. JVM is an abstract computing machine. Like a real computing machine, it has an instruction set. It can run code in Java class (bytecode) format.

At runtime, the class loader fetches the .class file from storage media or network. Each class file is fed to a bytecode verifier to ensure that the class file is correctly formatted and is safe for execution. The byte-verification process slows the process to load the class in JVM, but it is performed only once and not every time the program runs. Figure A.1 explains how the JVM fetches classes from disk or network and then verifies that the bytecodes are safe for executing.

Once the class is loaded (the bytecodes become available to JVM), the bytecodes in .class file are actually fed to the JVM. This bytecode is executed by one of the execution units (interpreter or JIT) of JVM at runtime. The execution units carry out the instructions specified in the bytecodes. The interpreter is the simplest execution unit of the JVM. It reads the bytecode, interprets it and performs the associated function. Interpreters are generally slower that the native-code compilers (like C++ compiler), as they need to lookup for meaning of each byte code during the execution. To speed up the execution, the JVM has another execution unit. It is called the JIT or “Just In Time” compiler. It optimizes the runtime bytecode from a class file by compiling it to native code just before the execution.