How Programs Work
Desktop/Laptop Systems
Now that you have written your first simple program, are you wondering how programs work on your desktop or laptop computer? It is important to understand the different parts of your computer and the role each part plays when your program is executed.
Although a computer contains many parts, the three most important building blocks are:
- CPU: Does the math and logic.
- RAM: Fast, temporary workspace for active data.
- HD: Slow, permanent storage for files and OS.

- Central Processing Unit (CPU): This is the brain of every computer. A computer must have one or more CPUs to be a computer! As the name suggests, this unit handles computational tasks. Programs containing statements like
2 + 5or2 * 4use the CPU to calculate the result. - Random Access Memory (RAM): This is the temporary memory of your computer. Any data stored in this memory is erased when the computer is shut down. RAM is the "playground" for your program! Your classes, methods, and variables are all loaded into RAM when a program starts. When the program finishes and exits, the memory used by your variables and objects is marked for deletion. When the computer shuts down, the RAM is wiped clean, allowing you to start with a fresh slate when you restart.
- Hard Disk (HD): This is the permanent memory of the computer. Anything saved on the hard disk remains even after the computer is restarted. Your
.javaand.classfiles are stored on the hard disk, as are the JDK and JRE. When the JRE runs your program, it spawns a Java Virtual Machine (JVM) and executes your program inside it. Every standalone program runs in its own JVM.
All of these computer resources are used not only by your program but also by the Operating System (OS) and other programs running on your machine, such as a web browser or a word processor.
- The OS is the manager of the computer. it coordinates hardware resources and provides a platform for other software (like the JVM) to run.
The OS decides, based on its scheduling algorithm, which program gets to use the CPU at any given time. Memory is shared by many programs simultaneously; your program will be allocated memory as required by the OS. Once your program finishes execution, its memory is released for use by other programs.

The CPU, RAM, and HD belong to the hardware layer of your computer—the physical components you can see and touch. Above this hardware layer, a special program called an Operating System (OS) is installed. There are many operating systems available today, most notably various versions of Windows, macOS, and Linux. Linux is an open-source OS.
Open-source software is a program whose source code is made available under a license that allows anyone to use, modify, and distribute it for any purpose, usually for free. Windows and macOS, however, are proprietary software. You typically have to pay to use them, and you cannot see or modify their source code.
The JDK and JRE are installed on top of the OS. Every OS has its own specific version of the JRE and JDK. The JRE spawns a Java Virtual Machine (JVM) on which your program runs. The four layers are shown in the figure above. When your program needs to access hardware components, it communicates through these layers until the OS directly accesses the hardware to perform the necessary tasks.