Chapter 1: Introduction to Java and the JVM
Metadata
| Field | Value |
|---|---|
| Chapter | 1 |
| Topic | Introduction to Java and the JVM |
| Estimated Time | 90-120 minutes |
| Outcome | Understand the Java execution model, the role of the JVM, and how to compile and run a first Java program |
1. Purpose
This chapter establishes the execution architecture of Java. The goal is to understand how Java source code becomes a running program and why the Java Virtual Machine (JVM) is central to that process. This foundation is required for all later topics, including object-oriented programming, memory management, exception handling, frameworks, and backend systems.
2. Problem Statement
Many beginners can write basic Java syntax without understanding how the code is executed. That gap usually causes problems later:
- debugging becomes mechanical instead of logical
- performance issues are hard to reason about
- backend and system-level concepts remain unclear
If the execution model is not clear at the beginning, later Java concepts often feel disconnected.
3. Core Definitions
3.1 Java
Java is a high-level, general-purpose programming language designed for readability, portability, and maintainability.
Key characteristics:
- object-oriented by design
- strongly typed
- compiled to bytecode rather than directly to machine code
- platform independent through the JVM
3.2 JDK (Java Development Kit)
The JDK is the full development package used to build Java applications.
It includes:
- the Java compiler,
javac - the Java launcher,
java - debugging and utility tools
- runtime components required during development
Use the JDK when writing, compiling, and running Java programs locally.
3.3 JRE (Java Runtime Environment)
The JRE provides the environment required to run Java applications.
It contains:
- the JVM
- standard Java libraries
- supporting runtime files
In modern Java distributions, the JDK is typically installed directly, and the runtime is included as part of it.
3.4 JVM (Java Virtual Machine)
The JVM is the runtime engine that executes Java bytecode.
Its responsibilities include:
- loading classes
- verifying bytecode
- managing memory
- executing instructions
- supporting garbage collection
The JVM is the layer that allows the same compiled Java bytecode to run on different operating systems.
4. Java Execution Pipeline
Java programs do not execute directly from source code. They move through a defined pipeline:
Source Code (.java)
|
| javac
v
Bytecode (.class)
|
| JVM
v
Machine-level execution
|
v
Program output
Execution Rules
- Java source files are not executed directly by the operating system.
- Java source code must be compiled into bytecode first.
- The JVM is required to execute Java bytecode.
- Bytecode is platform-neutral, which is what enables Java portability.
5. System Model
The Java execution model can be summarized as:
Java Code -> Compiler -> Bytecode -> JVM -> Machine Execution
Component Mapping
| Component | Role |
|---|---|
| Java code | Human-readable source written by the developer |
| Compiler | Translates source code into bytecode |
| Bytecode | Platform-neutral instruction format |
| JVM | Loads, verifies, and executes bytecode on the target system |
Mental Model
Think of Java as a two-stage execution system:
- The compiler translates source code into an intermediate format.
- The JVM translates and executes that format on the target machine.
That is the practical meaning of "write once, run anywhere."
6. Program Structure
6.1 Entry Point
The standard entry point for a Java application is:
public static void main(String[] args)
This signature matters because the JVM looks for it when starting a standalone program.
Entry Point Rules
- the method must be named
main - the return type must be
void - the parameter type must be
String[] - the method must be
publicandstatic
If the signature does not match, the JVM cannot start the program as a normal Java application.
6.2 Example Program
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Lanos!");
}
}
Structure Breakdown
| Element | Description |
|---|---|
public class Main |
Declares a public class named Main |
main(...) |
Defines the program entry point |
System.out.println(...) |
Writes a line of text to standard output |
7. Build and Execution Process
Step 1: Save the Source File
Save the program as:
Main.java
Step 2: Compile the Source Code
javac Main.java
This produces:
Main.class
Step 3: Run the Compiled Program
java Main
Expected Output
Hello, Lanos!
Why java Main and not java Main.java?
The java command runs the compiled class by its class name. After compilation, the JVM loads Main.class, not the source file.
8. Constraints and Rules
The following rules must be followed for a basic Java program to execute correctly:
- the file name must match the public class name
- source code must compile before it can run
- the JVM requires a valid
main()method for standalone execution - the class name used in the
javacommand must match the compiled class
9. Failure Scenarios
Understanding failure cases is part of understanding the system.
9.1 Invalid Entry Point
public static void main(String args)
Result:
- the JVM cannot identify the method as a valid entry point
- the program does not start normally
Correct form:
public static void main(String[] args)
9.2 File and Class Name Mismatch
public class Test {
}
Saved as:
Main.java
Result:
- compilation fails because a public class must match the file name
Correct rule:
public class Testmust be saved asTest.java
9.3 Attempting to Run Without Compilation
Command:
java Main
Before Main.class exists, the result is:
- class not found or class loading failure
Correct sequence:
- compile with
javac Main.java - run with
java Main
10. JVM Responsibilities
The JVM is more than a simple launcher. It is a runtime system with several core responsibilities:
- class loading
- bytecode verification
- memory allocation
- stack and heap management
- instruction execution
- garbage collection
This is why JVM behavior matters in real backend systems. Frameworks such as Spring Boot run on top of the JVM, so performance, memory usage, and runtime behavior all depend on it.
11. Implementation Task
Task: Info Printer
Create a Java program that prints three values:
- name
- goal
- learning status
Example Output
Name: Pavan
Goal: Build Lanos
Status: Learning Java Systematically
Requirements
- use a valid Java class and
main()method - compile successfully with
javac - execute successfully with
java - produce readable, labeled output
Reference Implementation
public class Main {
public static void main(String[] args) {
System.out.println("Name: Pavan");
System.out.println("Goal: Build Lanos");
System.out.println("Status: Learning Java Systematically");
}
}
12. Validation Checklist
Use this checklist before moving to the next chapter:
- program compiles without errors
- program executes successfully
- the difference between source code and bytecode is clear
- the role of the JVM is understood
- the compile-then-run workflow is understood
- the
main()method can be identified and explained
13. Knowledge Validation
Concept Review
| Prompt | Correct Answer |
|---|---|
| Java compiles into | Bytecode |
| JVM executes | Bytecode |
| Standard entry point | public static void main(String[] args) |
| Compiled file extension | .class |
Output Evaluation
Code:
System.out.println("A" + "B");
Expected output:
AB
Reason:
- the
+operator concatenates strings in this expression
Edge Case
Condition:
- the
main()method is removed from a standalone Java program
Result:
- the class may still compile if the syntax is valid
- the program will not run as a normal application because the JVM cannot find a valid entry point
14. Summary
This chapter introduced the basic execution architecture of Java:
- Java source code is written in
.javafiles - the compiler converts source code into
.classbytecode - the JVM executes that bytecode
- bytecode is the reason Java is portable across operating systems
- the
main()method is the standard entry point for standalone programs
Final Model
Java -> Bytecode -> JVM -> Machine Execution
The most important takeaway is that Java does not run directly on the machine. Java runs on the JVM, and the JVM bridges Java code to the underlying system.
