← Back to Chapters

Java Compilation & Execution

⚙️ Java Compilation & Execution

? Quick Overview

Java programs follow a two-step process: compilation and execution. Source code written by the programmer is first converted into bytecode, which is then executed by the Java Virtual Machine (JVM).

? Key Concepts

  • Java source files use the .java extension
  • Compilation is done using the javac compiler
  • Compiled bytecode is stored in .class files
  • Execution is handled by the JVM using the java command

? Syntax & Theory

The Java compiler checks syntax errors and converts source code into platform-independent bytecode. This bytecode runs on any system that has a compatible JVM installed.

? View Code Example
// Simple Java program to demonstrate compilation and execution
class HelloJava {
public static void main(String[] args) {
System.out.println("Java Compilation and Execution");
}
}

▶️ Live Output / Explanation

Program Flow

  1. Save file as HelloJava.java
  2. Compile using: javac HelloJava.java
  3. Run using: java HelloJava

Output: Java Compilation and Execution

✅ Tips & Best Practices

  • Class name must match the filename exactly
  • Always check for compilation errors before execution
  • Ensure JDK is properly installed and configured

? Try It Yourself

  • Modify the program to print your name
  • Add another class and try compiling both
  • Observe what happens if class name and file name differ