Back to the 2020 paper

Module III: Basics of Web Programming

20207m

Explain the steps involved in creating and executing a Java program.

Worked SolutionAI Assisted

Answer: Steps Involved in Creating and Executing a Java Program

1. Introduction

Java follows the "Write Once, Run Anywhere" (WORA) philosophy. Unlike traditional compiled languages (like C/C++) that compile directly into platform-dependent machine binaries, Java source code is compiled into an intermediate, platform-neutral format called Bytecode, which is executed by the Java Virtual Machine (JVM).

+---------------------+
|  Java Source Code   |  (HelloWorld.java)
+----------+----------+
           |
           | 1. javac Compiler
           v
+---------------------+
|    Java Bytecode    |  (HelloWorld.class)
+----------+----------+
           |
           | 2. Transferred to any OS (Windows, Linux, macOS)
           v
+---------------------------------------------------------------+
|                  JAVA VIRTUAL MACHINE (JVM)                   |
|                                                               |
|  +----------------------+      +---------------------------+  |
|  | ClassLoader Subsystem| ---> |   Bytecode Verifier       |  |
|  +----------------------+      +-------------+-------------+  |
|                                              |                |
|                                              v                |
|                                +---------------------------+  |
|                                |     Execution Engine      |  |
|                                | (Interpreter + JIT Engine)|  |
|                                +-------------+-------------+  |
+----------------------------------------------|----------------+
                                               |
                                               v
                                     +--------------------+
                                     | Machine Code / CPU |
                                     +--------------------+

2. Step-by-Step Process

Step 1: Writing the Source Code

  • Create a Java source file using any text editor or IDE (VS Code, IntelliJ IDEA, Eclipse).
  • Save the file with a .java extension. The file name must exactly match the public class name.
// File: HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World from Java!");
    }
}

Step 2: Compiling the Source Code (javac)

  • The Java Compiler (javac) reads the .java source code, verifies syntax and semantic rules, and translates it into platform-independent Bytecode.
  • Command:
    javac HelloWorld.java
    
  • Output: Produces a binary file named HelloWorld.class.

Step 3: Loading Classes into Memory (ClassLoader)

When executing the program (java HelloWorld), the JVM starts and its ClassLoader subsystem performs three phases:

  1. Loading: Reads .class binary streams and creates the Class object in the JVM Method Area.
  2. Linking:
    • Verification: Ensures bytecode adheres to JVM specifications and has no illegal memory accesses.
    • Preparation: Allocates memory for static variables and initializes default values.
    • Resolution: Replaces symbolic references with direct memory references.
  3. Initialization: Executes static initializers and assigns explicit static values.

Step 4: Bytecode Verification

  • The Bytecode Verifier checks the code for security violations (e.g., stack overflow, illegal data type casting, unauthorized pointer manipulation) before execution.

Step 5: Execution by the JVM Execution Engine

The JVM execution engine executes bytecode instructions on the underlying CPU using two combined strategies:

  1. Interpreter: Reads and executes bytecode instructions line by line (starts quickly).
  2. JIT (Just-In-Time) Compiler: Identifies frequently executed code blocks ("hot spots") and compiles them directly into native machine code to drastically boost runtime performance.
  3. Garbage Collector: Automatically tracks and reclaims unreferenced heap memory in the background.

3. Summary of CLI Commands

Step Command Tool Used File Produced
Compilation javac HelloWorld.java Java Compiler (javac) HelloWorld.class (Bytecode)
Execution java HelloWorld Java Virtual Machine (java) Console Output / Runtime

Similar questions