Back to the 2020 paper
Step 2: Compiling the Source Code (
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
.javaextension. 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.javasource 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:
- Loading: Reads
.classbinary streams and creates theClassobject in the JVM Method Area. - 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.
- 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:
- Interpreter: Reads and executes bytecode instructions line by line (starts quickly).
- 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.
- 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
Web TechnologyThe command to execute a compiled Java program is (i) javac (ii) java (iii) run (iv) execute20192mWeb TechnologyThe Java compiler (i) creates executable (ii) translates Java source code to byte code (iii) creates classes (iv) produces Java interpreters20192mWeb TechnologyList the applications of object-oriented programming. What is the significance of Java's byte code?20207mWeb TechnologyExplain Java garbage collection mechanism.20207m