Programming and Java Fundamentals

How a Computer Executes a Program

ReadingPreview

আপনি একটি free preview lesson দেখছেন।

Lesson Overview

The source code we write in Java cannot be executed directly by the computer's processor.

Java source code is first compiled into bytecode. Then the Java Virtual Machine (JVM) executes that bytecode and works with the underlying operating system and hardware.

In this lesson, we will learn:

  • The basic roles of CPU, memory, and storage
  • Machine language and high-level programming languages
  • Compiler and runtime execution
  • How Java source code is executed
  • The roles of bytecode, JVM, and JIT
  • How Java can run on different platforms

Learning Objectives

After completing this lesson, you will be able to:

  • Explain the roles of CPU, memory, and storage
  • Understand the difference between machine language and high-level languages
  • Explain what a compiler does
  • Explain the flow from Java source code to a running program
  • Understand the difference between .java and .class files
  • Explain why bytecode and the JVM are needed

What Happens in a Computer When a Program Runs?

When a program runs, several parts of hardware and software work together.

The most important ones for us are:

  • CPU
  • Memory
  • Storage
  • Operating System

A simplified view of program execution:

Program is stored in storage
        ↓
Program is loaded into memory
        ↓
CPU executes instructions
        ↓
Program produces a result

CPU

The CPU — Central Processing Unit executes computer instructions.

A CPU can perform different types of operations:

  • Calculations
  • Comparisons
  • Data movement
  • Reading data from memory
  • Writing data to memory
  • Selecting instructions based on conditions

Suppose we have:

int firstNumber = 10;
int secondNumber = 20;
int total = firstNumber + secondNumber;

The programmer only wrote:

firstNumber + secondNumber

But at a lower level, the computer may need to perform multiple operations such as loading values, adding them, and storing the result.

One major advantage of using a high-level programming language is that the programmer does not need to write these low-level instructions directly.

Memory and Storage

While a program is running, RAM is generally used to store temporary data.

Example:

int age = 30;
String name = "Sakib";

While the program is running, the data required for these values is stored in memory.

RAM is volatile. Its data does not remain when the computer is powered off.

In contrast, storage keeps long-term data.

Examples:

  • SSD
  • HDD
  • External drive

Storage may contain:

  • Operating system
  • Application
  • Java source file
  • Compiled file
  • Database
  • Image
  • Document

Difference Between Memory and Storage

TopicMemoryStorage
Common exampleRAMSSD / HDD
PurposeTemporary data for running programsLong-term data
SpeedGenerally fasterComparatively slower
When power is offData is lostData remains
ExampleVariable, objectFile, application

Simply put:

Programs are stored in storage and run in memory.

Operating System

The operating system manages the computer's hardware resources and running programs.

Examples:

  • Windows
  • macOS
  • Linux

Responsibilities of an operating system include:

  • Managing CPU usage
  • Allocating memory
  • Managing the file system
  • Managing running processes
  • Managing network and device resources

When a Java application runs, the operating system runs a Java process and provides it with the CPU and memory resources it needs.

What Kind of Instructions Does a Computer Understand?

At the lowest level, information in a computer is represented in binary form.

The binary system uses:

0
1

Low-level instructions that a processor can execute directly are called machine instructions or machine language.

Machine instructions can depend on the processor architecture.

Examples:

  • x86-64
  • ARM

Writing large programs directly in machine language is extremely difficult for humans.

This is why we use high-level programming languages.

High-Level Programming Language

Java is a high-level programming language.

Example:

int total = firstPrice + secondPrice;

By reading this code, we can easily understand that two prices are being added and the result is stored in total.

High-level languages provide:

  • Readable code
  • Easier maintenance
  • Faster development
  • Reusable abstractions
  • Separation from low-level hardware details

Java, Python, Go, and JavaScript are examples of high-level programming languages.

The CPU Does Not Directly Execute Source Code

Java source code is human-readable.

Example:

int total = 10 + 20;

But the CPU does not directly understand Java syntax.

Java source code must be translated into an intermediate format that the Java runtime can execute.

The first step in this process is compilation.

Compiler

A compiler is a program that analyzes source code and translates it into another format.

The Java compiler can detect different kinds of problems while checking source code.

Example:

int age = "twenty";

Here, a text value is being assigned to an int variable.

The Java compiler will not allow this code to compile.

The compiler can detect many syntax and type-related errors before the program runs.

Compilation

The process of converting source code into a translated format using a compiler is called compilation.

General concept:

Source Code
    ↓
Compiler
    ↓
Translated Code

In Java:

Java Source Code
        ↓
Java Compiler
        ↓
Java Bytecode

Compiled and Interpreted Execution

Classifying programming languages only as compiled or interpreted is not always completely accurate.

In simplified terms:

Compilation

Source code is translated into another format before the program executes.

Interpretation

Instructions are read and executed at runtime.

Modern runtimes may use compilation, interpretation, and runtime optimization together.

Java also uses such a hybrid execution model.

How Does a Java Program Execute?

A simplified Java program execution flow:

Java Source Code (.java)
        ↓
Java Compiler (javac)
        ↓
Java Bytecode (.class)
        ↓
JVM
        ↓
Operating System / CPU
        ↓
Program Output

Step 1: Java Source Code

Java source code is generally written in a .java file.

Example:

Main.java

File:

public class Main {

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

This is human-readable source code written by the developer.

Step 2: Compile the Source Code

The Java compiler command is:

javac Main.java

If compilation is successful, it creates:

Main.class

The Main.class file contains Java bytecode.

Step 3: Bytecode

Bytecode is an intermediate instruction format created by the Java compiler.

It is:

  • Not Java source code
  • Not direct CPU machine code
  • An instruction format created for the JVM

Flow:

Main.java
    ↓
javac
    ↓
Main.class

Step 4: JVM Runs the Program

The JVM — Java Virtual Machine is the runtime environment that executes Java bytecode.

To run the program:

java Main

The JVM loads the Main class, and execution of a standalone Java application begins from the main method.

Output:

Hello Java

Notice:

java Main

The .class extension is not included in the command.

Role of the JVM

The main responsibility of the JVM is to execute bytecode.

In addition, the JVM provides various runtime services, such as:

  • Class loading
  • Bytecode execution
  • Memory management
  • Garbage collection
  • Thread management
  • Runtime optimization

You do not need to understand the JVM's internal architecture in detail yet. We will discuss the necessary parts later as we continue learning Java.

JIT Compiler

Modern JVMs can use a JIT — Just-In-Time Compiler to improve runtime performance.

In simplified form:

Bytecode
    ↓
JVM execution
    ↓
Frequently executed code
    ↓
JIT compilation
    ↓
Native machine code

The JVM can use runtime information to optimize frequently executed code.

This is why Java execution is not only traditional interpretation; compilation and runtime optimization can work together.

Java Platform Independence

The Java compiler generally produces bytecode instead of a platform-specific machine executable.

The same bytecode can run on different platforms when a compatible JVM is available.

                Windows JVM
              ↗
Java Bytecode → Linux JVM
              ↘
                macOS JVM

The important points are:

  • Bytecode can be portable
  • The JVM is platform-specific

A JVM for Windows works with Windows, while a JVM for Linux works with Linux.

This model forms the basis of Java's portability.

This idea in Java is often expressed as:

Write once, run anywhere.

However, if an application uses operating-system-specific paths, native libraries, or platform-specific dependencies, additional compatibility considerations may be required.

.java and .class

When learning Java, it is important to clearly remember these two file types.

.java

Source code written by the developer.

Main.java

.class

Bytecode generated by the Java compiler.

Main.class

Flow:

Main.java
    ↓
javac
    ↓
Main.class

Compilation and Execution

These are two separate stages.

Compilation

Main.java → Main.class

The source code is checked and bytecode is generated.

Command:

javac Main.java

Execution

Main.class → JVM → Running Program

Command:

java Main

Simply put:

Compilation prepares the code, while execution runs the code.

Compilation Error vs. Runtime Error

Understanding this difference is important.

Compilation Error

The following code will not compile:

System.out.println("Hello")

The semicolon is missing.

Correct:

System.out.println("Hello");

The compiler detects the problem before the program runs.

Runtime Error

The following code can compile:

int result = 10 / 0;

But it will fail during execution.

That means:

Successful compilation does not mean that a program will be error-free at runtime.

What Happens When You Press Run in an IDE?

An IDE such as IntelliJ IDEA performs many tasks automatically.

In simplified form:

Source Code
    ↓
Compile
    ↓
Bytecode
    ↓
JVM Start
    ↓
Program Execute
    ↓
Console Output

An IDE makes development easier, but the Java compiler compiles Java code and the JVM executes the bytecode.

Relationship Between Hardware, OS, and JVM

A Java application can be thought of in layers:

Java Application
        ↓
JVM
        ↓
Operating System
        ↓
Hardware

Java Application

The developer's program logic.

JVM

Executes Java bytecode and provides runtime services.

Operating System

Manages processes, memory, files, and hardware resources.

Hardware

CPU, RAM, storage, and physical devices.

Because of this abstraction, a Java developer generally does not need to work with processor-specific instructions when writing normal application code.

Why Do We Need to Know These Concepts?

This foundation will help you understand:

  • Why the CPU does not directly execute Java code
  • Why .java and .class files are different
  • Where compilation errors occur
  • Where runtime errors occur
  • Why the JVM is needed
  • How Java can run on different operating systems
  • What happens behind an IDE's Run button

Important Terms

CPU

The processing unit that executes computer instructions.

Memory

The place where temporary data for running programs is stored.

Storage

The place where long-term files and data are stored.

Operating System

System software that manages computer resources and running programs.

Machine Language

Low-level instructions that a processor can execute directly.

High-Level Language

A programming language that is comparatively readable for humans.

Compiler

A program that analyzes source code and translates it into another format.

Compilation

The process of translating source code.

Bytecode

An intermediate instruction format produced by the Java compiler that is compatible with the JVM.

JVM

The Java Virtual Machine, which executes Java bytecode and provides runtime services.

JIT Compiler

A compiler that can compile selected bytecode into native machine code at runtime for optimization.

Source File

A .java file written by the developer.

Class File

A compiled .class file containing Java bytecode.

Practice Exercise 1: Execution Order

Arrange the following steps in the correct order:

  • JVM executes the bytecode
  • Java source code is written
  • The program produces output
  • Java compiler generates bytecode

Practice Exercise 2: Compilation or Runtime?

Determine whether each situation is a problem during the compilation stage or runtime stage.

Situation 1

A semicolon is missing at the end of a statement.

Situation 2

The program divides an integer value by zero.

Situation 3

A text value is assigned to an int variable.

Situation 4

The file that the program is trying to open cannot be found.

Practice Exercise 3: Java Execution Flow

Write the Java execution flow using the following terms:

  • Source code
  • Java compiler
  • Bytecode
  • JVM
  • CPU
  • Output

Knowledge Check

Question 1

What is the main difference between memory and storage?

Question 2

Why do we use high-level programming languages?

Question 3

What does a compiler do?

Question 4

What are the extensions of a Java source file and a compiled file?

Question 5

What is bytecode?

Question 6

What is the role of the JVM?

Question 7

What is the difference between compilation and execution?

Question 8

How does Java's platform independence work?

Knowledge Check Answers

Answer 1

Memory stores temporary data for running programs. Storage preserves long-term files and data.

Answer 2

A high-level language makes code readable and maintainable for humans and removes the need for programmers to write low-level processor instructions directly.

Answer 3

A compiler analyzes source code and translates it into another format. The Java compiler produces bytecode from Java source code.

Answer 4

Java source file:

.java

Compiled bytecode file:

.class

Answer 5

Bytecode is an intermediate instruction format produced by the Java compiler that the JVM can execute.

Answer 6

The JVM executes Java bytecode and provides services such as memory management, class loading, and runtime optimization.

Answer 7

Compilation creates bytecode from source code. During execution, the JVM runs that bytecode.

Answer 8

The Java compiler produces bytecode instead of a platform-specific executable. When a compatible platform-specific JVM is available, the same bytecode can run on different operating systems.

Lesson Summary

In this lesson, we learned:

  • The CPU executes computer instructions
  • Memory stores temporary data for running programs
  • Storage keeps long-term data
  • The operating system manages hardware resources and processes
  • The CPU does not directly execute Java source code
  • Java is a high-level programming language
  • The Java compiler produces .class bytecode from .java source code
  • The JVM executes bytecode
  • JIT can optimize selected code at runtime
  • Compilation and execution are separate stages
  • Java bytecode can run on different platforms when a compatible JVM is available