Java code compilation and execution

Introduction

A Java program file with a .java extension is converted by the javac compiler into a bytecode file with a .class extension. The resulting .class file can be executed on any platform that has an appropriate JVM, as shown in the following figure,

Java code execution
Java code compilation

The following figure shows the work of the JRE during code execution,

Java code execution
Java code execution

The .class file generated by the compiler is independent of the target system’s hardware or software platform. The .class file containing the main class is passed to the JVM. Inside the JVM, the file goes through certain components, which are as follows,

Class Loader

Once the main class is loaded into JVM memory, the other classes referenced in the program are loaded through the class loader.

Bytecode Verifier

The loaded class bytecode checks for corrupted instructions. The checks performed by the bytecode checker are as follows,

  • Variable initialization before use.
  • Method calls and object reference types must match.
  • No violation of rules of access to private methods and data.
  • Access to a local variable is in the runtime stack.
  • There must be no stack overflow at runtime.

If any of the above checks fail, the verifier will not allow the class to load. The interpreter then executes the bytecode.

Just-In-Time (JIT) Compiler

Since Java 2.0, the JIT compiler has been included at runtime to improve execution speed. The task of the JIT compiler is to convert bytecode into native or platform-specific code to improve performance.

Leave a Comment