Skip to content

Latest commit

 

History

History
292 lines (191 loc) · 7.49 KB

File metadata and controls

292 lines (191 loc) · 7.49 KB

How a Java File Runs (How Java Works)

1. Overview

Java follows the principle of “Write Once, Run Anywhere” (WORA). This is made possible through the Java Virtual Machine (JVM) which allows Java programs to run on any platform.

When you write and run a Java program, it goes through multiple steps:

  1. Writing the code (.java file)

  2. Compilation (.class file → bytecode)

  3. Execution (JVM interprets bytecode)


2. Steps in Java Program Execution

The complete flow from source code to execution is as follows:

.java  →  javac compiler  →  .class (bytecode)  →  JVM  →  Machine Code

Each step has a specific role, explained below.


3. Step 1 — Writing the Source Code

You first write Java code in a .java file using a text editor or IDE.

Example:

class Hello {
    public static void main(String[] args) {
        System.out.println("Hello Java");
    }
}

File saved as:

Hello.java

4. Step 2 — Compilation (javac)

The Java Compiler (javac) converts the source code (.java) into bytecode (.class).

Command:

javac Hello.java

This generates:

Hello.class

Key Points:

  • Checks syntax and semantics.

  • Verifies class names and package structure.

  • Does not convert to native machine code.

  • Produces platform-independent bytecode.


5. Step 3 — Bytecode (.class File)

The output of the compilation process is a .class file. This file contains Java bytecode — instructions understandable by the JVM.

Bytecode is not machine-dependent, making Java platform-independent.

Example (bytecode view using javap -c Hello):

public static void main(java.lang.String[]);
Code:
   0: getstatic     #2   // Field java/lang/System.out:Ljava/io/PrintStream;
   3: ldc           #3   // String Hello Java
   5: invokevirtual #4   // Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8: return

6. Step 4 — Execution (JVM)

When you run the program:

java Hello

The Java Virtual Machine (JVM) loads the .class file, verifies bytecode, and executes it.

The Execution Steps:

  1. Class Loader: Loads .class files into memory.

  2. Bytecode Verifier: Checks code for security and correctness.

  3. Interpreter / JIT Compiler: Converts bytecode into machine code for execution.

  4. Runtime Execution: Executes the native code on the host machine.


7. JVM Internal Architecture

The Java Virtual Machine performs all runtime activities.

Main Components of JVM:

Component Description
Class Loader Loads classes dynamically at runtime.
Bytecode Verifier Ensures bytecode follows security and language rules.
Interpreter Reads and executes bytecode line by line.
JIT Compiler Compiles frequently used bytecode into native machine code for faster execution.
Garbage Collector Frees unused memory automatically.
Runtime Data Areas Memory regions where data is stored during execution.

JVM Memory Areas

Memory Area Description
Method Area Stores class-level data (static variables, constants, method code).
Heap Stores objects and instance variables.
Stack Stores method frames, local variables, and references.
PC Register Holds address of current executing instruction.
Native Method Stack Stores native (non-Java) method calls.

8. Role of JIT (Just-In-Time) Compiler

The JIT Compiler improves performance by compiling bytecode to native machine code during execution.

How it Works:

  • Initially, JVM interprets bytecode.
  • Frequently executed sections (hot code) are compiled by JIT into native code.
  • Native code runs directly on CPU → improves speed.

9. Java Execution Flow — Visual Diagram

        +-------------------+
        |  .java Source File |
        +---------+---------+
                  |
                  v
        +-------------------+
        |   javac Compiler  |
        +---------+---------+
                  |
                  v
        +-------------------+
        |  .class Bytecode  |
        +---------+---------+
                  |
                  v
        +---------------------------+
        |      Java Virtual Machine |
        |  - Class Loader           |
        |  - Bytecode Verifier      |
        |  - Interpreter / JIT      |
        |  - Garbage Collector      |
        +---------------------------+
                  |
                  v
        +-------------------+
        |  Machine Execution |
        +-------------------+

10. Example Walkthrough

Consider:

class Example {
    public static void main(String[] args) {
        int x = 5;
        int y = 10;
        System.out.println(x + y);
    }
}

Execution Process:

  1. Save as Example.java.

  2. Compile → javac Example.java → produces Example.class.

  3. Run → java Example.

  4. JVM loads class, verifies, executes main().

  5. Output:

    15
    

11. Platform Independence Explained

Java is platform-independent at bytecode level because the .class file runs on any machine with a JVM.

Stage Platform Dependence
Source Code (.java) Platform-independent
Bytecode (.class) Platform-independent
Machine Code Platform-dependent

Different OS have different JVMs (Windows JVM, Linux JVM, Mac JVM), but all understand the same bytecode.


12. Key Components Summary

Component Responsibility
JDK Development tools (javac, jar, debugger, etc.)
JRE Runtime environment (JVM + libraries)
JVM Executes bytecode on the machine

13. Common Interview Questions

Q1: Why is Java platform-independent?

Because Java compiles source code into bytecode, which can run on any system with a JVM.

Q2: What does JVM do?

It converts bytecode into machine-specific code and executes it.

Q3: What is the difference between JVM, JRE, and JDK?

  • JDK: Development + Runtime tools
  • JRE: Runtime environment
  • JVM: Execution engine

Q4: What is bytecode?

A platform-independent intermediate representation of compiled Java code executed by JVM.

Q5: What is the role of the Class Loader?

It loads .class files dynamically into memory when required during execution.


14. Key Takeaways

  • Java programs go through Compilation → Bytecode → Execution stages.

  • javac converts .java into .class.

  • JVM executes bytecode and provides platform independence.

  • JIT Compiler boosts runtime performance.

  • Garbage Collector manages memory automatically.