Core Java Programming: From Beginner to Project Development

Lesson 1.9: Compilation and Execution Process

Introduction

Have you ever wondered what happens after you click the Run button in a Java program?

Does the computer understand Java code directly?

The answer is No.

A computer understands only machine language (binary code), but Java programs are written in human-readable source code. Therefore, Java follows a special process to convert your code into machine language.

In this lesson, you will learn how a Java program is compiled and executed step by step.


Learning Objectives

After completing this lesson, you will be able to:

  • Understand the Java compilation process.

  • Explain how Java programs are executed.

  • Understand Source Code, Bytecode, and Machine Code.

  • Explain the role of the Java Compiler and JVM.

  • Understand why Java is platform-independent.


Java Program Execution Process

The execution of a Java program consists of five main steps:

  1. Write the Source Code

  2. Compile the Program

  3. Generate Bytecode

  4. Execute Bytecode using the JVM

  5. Display the Output


Step 1: Write the Source Code

A Java program is written in a text file with the .java extension.

Example:

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

File Name:

Hello.java

This file is called the Source Code.


Step 2: Compile the Program

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

Command:

javac Hello.java

If there are no errors, the compiler creates a new file:

Hello.class

This file contains Java Bytecode.


What is Bytecode?

Bytecode is an intermediate code generated by the Java compiler.

It is not machine language.

Instead, it is a special code that can be understood by the Java Virtual Machine (JVM).

Because of bytecode, Java programs can run on different operating systems without changing the source code.


Step 3: Execute the Bytecode

Run the following command:

java Hello

The JVM loads the Hello.class file.

It then converts the bytecode into machine code that the computer’s processor can execute.

Finally, the program runs and displays the output.


Output

Hello Java

Java Compilation and Execution Flow

The complete process can be represented as follows:

Hello.java
      │
      ▼
Java Compiler (javac)
      │
      ▼
Hello.class (Bytecode)
      │
      ▼
Java Virtual Machine (JVM)
      │
      ▼
Machine Code
      │
      ▼
Output on Screen

Role of the Java Compiler

The Java Compiler (javac) performs several important tasks:

  • Checks syntax errors.

  • Converts source code into bytecode.

  • Creates the .class file.

  • Reports compilation errors.

If your program contains errors, the compiler stops the process and displays error messages.


Role of the JVM

The Java Virtual Machine (JVM) is responsible for:

  • Loading bytecode.

  • Verifying bytecode.

  • Converting bytecode into machine code.

  • Executing the program.

  • Managing memory.

  • Performing Garbage Collection.

  • Handling exceptions.

Without the JVM, Java programs cannot run.


Why is Java Platform Independent?

Different operating systems understand different machine languages.

Instead of generating machine code directly, Java generates bytecode.

Each operating system has its own JVM.

The JVM converts the same bytecode into machine code suitable for that operating system.

This is why Java follows the principle:

Write Once, Run Anywhere (WORA).


Common Compilation Errors

1. Missing Semicolon

Incorrect:

System.out.println("Hello")

Correct:

System.out.println("Hello");

2. File Name and Class Name Do Not Match

Incorrect:

File:

Demo.java

Class:

public class Hello

Correct:

File:

Hello.java

Class:

public class Hello

3. Missing Curly Braces

Every opening brace { must have a matching closing brace }.


4. Incorrect Capitalization

Incorrect:

system.out.println("Hello");

Correct:

System.out.println("Hello");

Java is case-sensitive.


Difference Between Compilation and Execution

Compilation Execution
Converts source code into bytecode Runs the bytecode
Performed by the Java Compiler (javac) Performed by the JVM
Produces a .class file Displays the program output
Reports syntax errors Reports runtime errors

Best Practices

  • Save the file before compiling.

  • Ensure the file name matches the public class name.

  • Read compiler error messages carefully.

  • Correct all compilation errors before running the program.

  • Keep your code properly formatted.


Key Points to Remember

  • Java source files have the .java extension.

  • The Java Compiler converts source code into bytecode.

  • Bytecode is stored in a .class file.

  • The JVM executes the bytecode.

  • Java programs are platform-independent because of bytecode and the JVM.


Lesson Summary

In this lesson, you learned:

  • The complete Java compilation and execution process.

  • The roles of the Java Compiler and JVM.

  • The meaning of source code, bytecode, and machine code.

  • Why Java is platform-independent.

  • Common compilation errors and how to fix them.

Understanding this process will help you troubleshoot Java programs and understand how Java works behind the scenes.


Practice Questions

  1. What is source code?

  2. What is bytecode?

  3. Which command compiles a Java program?

  4. Which command executes a Java program?

  5. What is the role of the Java Compiler?

  6. What is the role of the JVM?

  7. Why is Java platform-independent?

  8. What file is created after successful compilation?


Programming Exercise

  1. Create a file named Test.java.

  2. Write a program that prints “Java Programming is Fun!”.

  3. Compile the program using the command prompt.

  4. Run the program.

  5. Observe the generated .class file.


Assignment

Perform the following tasks:

  1. Write a Java program that displays your name and city.

  2. Compile the program using javac.

  3. Execute it using java.

  4. Identify the generated .class file.

  5. Write a short explanation (100–150 words) describing the compilation and execution process in your own words.

 

Scroll to Top