Topic 0.1
What a Program Is
In one line
A program is a list of exact instructions that a computer follows one after another, very fast and without ever guessing what you meant. A programming language like Java lets humans write those instructions in words, and a tool translates them into the numbers the computer's chip understands.
Think of it like this
A recipe for a cheese sandwich. "Take two slices of bread. Spread butter on one slice. Put cheese on the butter. Close the sandwich." Now imagine giving that recipe to a robot that does exactly what the words say and nothing else. If you forget to write "close the sandwich", the robot hands you an open sandwich. If you swap two steps, it puts cheese on the plate and butter on the cheese. A program is a recipe for a computer, and the computer is that very fast, very literal robot.
Words you'll meet
New words in this topic, in plain English. Come back here whenever one feels fuzzy.
- Program
- A list of exact instructions that a computer follows, one after another.
- Instruction
- One single step in a program, like "print this line" or "add these two numbers".
- CPU
- Central processing unit: the chip inside a computer that carries out instructions, billions per second.
- Memory (RAM)
- Fast, temporary storage where a running program keeps its values. It is wiped when the computer turns off.
- Machine code
- Instructions written as numbers that a CPU understands directly. Each kind of CPU has its own machine code.
- Programming language
- A precise, human-readable way to write instructions, such as Java, Python or C. A tool translates it for the machine.
- Source code
- The text of a program as a human writes it, saved in a file. Java source files end in
.java. - Output
- What a program produces, for example lines of text printed on the screen.
- Bug
- A mistake in a program that makes it fail or give the wrong result.
Step by step
01Instructions, in order
A program is a recipe. Each line is one step, and the computer does the steps in the order they are written. It does not skip ahead, it does not get bored, and it does not fill in missing steps.
Here is the sandwich recipe as a Java program. You don't need to understand every word yet (Topic 0.5 explains each one). For now, notice that each System.out.println(...) line means "print this text on the screen, then move to a new line", and that the lines run from top to bottom.
public class Main {
public static void main(String[] args) {
System.out.println("Step 1: Take two slices of bread.");
System.out.println("Step 2: Spread butter on one slice.");
System.out.println("Step 3: Put cheese on the butter.");
System.out.println("Step 4: Close the sandwich.");
}
}02What is inside a computer
Three parts matter for programmers. The CPU does the work, one tiny instruction at a time, billions of times per second. Memory (RAM) holds the values a running program is using right now, such as your score in a game. Storage (the disk or SSD) keeps files, including your program's file, even when the power is off.
When you run a program, its instructions are copied from storage into memory, and the CPU reads them from memory one by one. Values the program creates, like the number 12, also live in memory while the program runs. When the program ends, that memory is handed back.
03The CPU only understands numbers
A CPU has no idea what "print" or "sandwich" means. It understands machine code: instructions encoded as numbers, like 01001000 10000011 11000000 00000101, which might mean "add 5 to a register" (a register is a tiny storage box inside the CPU).
Writing programs that way would be slow and painful, and the result would only run on one kind of chip. An Intel or AMD chip (called x86-64) and an Apple M-series or phone chip (called ARM) have different machine codes. A program written in one chip's numbers would not run on the other.
04Programming languages are a bridge
A programming language gives you words and rules that are easy for people to read but have exactly one meaning, so a tool can translate them without guessing. total = price + tax; is clear to you and to the translator.
There are two classic ways to translate. A compiler translates the whole program ahead of time into another form, then you run that result. An interpreter reads your program and carries it out line by line while it runs. Java uses both ideas: the javac compiler turns your code into bytecode (a portable instruction format), and then the Java Virtual Machine interprets that bytecode and compiles the busy parts into real machine code while the program runs. Topics 0.2 and 0.8 show this in detail.
05Input, processing, output
Almost every program has the same shape. It starts with some input (data), does some processing (work on the data), and produces output (a result).
In the program below the input is two fixed numbers, the processing is a division, and the output is three printed lines. int cookies = 12; creates a named box called cookies and puts 12 in it (Phase 1 covers these boxes, called variables, in depth). The / sign divides. The + between text and a number glues them into one line of text.
public class Main {
public static void main(String[] args) {
int cookies = 12; // input
int friends = 4; // input
int each = cookies / friends; // processing
System.out.println("Each friend gets: " + each); // output
}
}06Order matters
Because instructions run top to bottom, a value can change as the program goes. Below, money starts at 10, grows by 5, then shrinks by 3. Each print shows the value at that moment.
Swap the two middle lines and the final answer stays 12, but the printed line in between becomes different. In bigger programs, swapping lines can change the final answer too, for example printing a total before you finish adding to it.
int money = 10;
money = money + 5; // a birthday gift
System.out.println("After gift: " + money); // 15
money = money - 3; // buy a snack
System.out.println("After snack: " + money); // 1207Bugs: the computer does what you wrote
If you meant to share 12 cookies among 4 friends but typed cookies / 3, Java happily prints 4. Nothing crashes. The program is wrong, but only you can tell, because only you know what you meant.
That is why programmers check their programs with examples whose answers they already know (12 cookies and 4 friends must give 3). Later in the course you'll write automated checks called unit tests (Topic 15.5). The other kind of bug, where the code breaks Java's rules, is caught by the compiler before the program even runs. Topic 0.7 teaches you to read those messages.
Try it yourself
- 1
Change the recipe
Open the first example and press Run. Then add a line
System.out.println("Step 5: Cut it in half.");before the "Done!" line. Predict the output, then run it. Next, move Step 1 to the bottom and run again. The program runs exactly the order you wrote, even when it makes no sense. - 2
Predict before you run
In the cookie example, change
cookiesto20andfriendsto6. Write down what you expect for "Each friend gets" and "Left in the jar" before you press Run. (Answer: 3 and 2, because 6 × 3 = 18 and 20 − 18 = 2.) - 3
Make the money example go negative
Change the snack price from
3to30. Predict the last two lines. Java does not stop you from spending money you don't have: it prints-15and then-30. Checking for that is your job, with anif(Topic 2.1).
Code & diagrams
Each println prints one line. The lines appear in exactly the order they are written.
Expected output
Step 1: Take two slices of bread.
Step 2: Spread butter on one slice.
Step 3: Put cheese on the butter.
Step 4: Close the sandwich.
Done! Enjoy your sandwich.The text after // is a comment: a note for humans that Java ignores. The % sign gives the remainder of a division.
Expected output
Cookies: 14
Friends: 4
Each friend gets: 3
Left in the jar: 2Expected output
Start: 10
After gift: 15
After snack: 12
After doubling: 24Break it on purpose
Errors are the best teachers. Make each change, read the error, guess what went wrong, then reveal the answer.
Break #1
Forget the quotes around text
In the recipe example, remove the quotes so a line reads System.out.println(Step 1: Take two slices of bread.);
Break #2
A wrong answer with no error
In the cookie example, change cookies / friends to cookies / 3.
Myth vs fact
Myth
Computers are smart and understand what I want.
Fact
A computer understands nothing. It follows instructions exactly as written, very quickly. All the smartness is in the instructions people write.
Myth
You need to be good at maths to program.
Fact
Most everyday programming needs only basic arithmetic and clear step-by-step thinking. Specialised fields (graphics, machine learning) use more maths, but you can learn that when you need it.
Myth
If a program runs without errors, it is correct.
Fact
Running without errors only means it follows the language's rules. It can still compute the wrong answer. Testing with known answers is how you find logic bugs.
Pro corner
Extra depth for experienced readers. New to this? Skip it for now and come back later.
- ▸
The input → processing → output model is the same one used to reason about whole systems: a web request is input, your service processes it, the response is output. The System Design course (
/) starts from exactly this picture. - ▸
"Compiled vs interpreted" is a property of an implementation, not of a language. Java source is compiled ahead of time to bytecode, then the JVM both interprets and JIT-compiles that bytecode (Topic 0.8). Python's main implementation compiles to its own bytecode too, and there are ahead-of-time Java compilers such as GraalVM Native Image.
- ▸
"Top to bottom" is the order the program appears to run in. The compiler, the JIT and the CPU are allowed to reorder work as long as a single thread cannot tell the difference. Once several threads share memory, that reordering becomes visible, which is what the Java Memory Model (Topic 13.4) is about.
Remember this
- 1
A program is a list of instructions. The computer runs them in order, top to bottom, unless an instruction tells it to jump somewhere else (you'll learn those jumps,
ifand loops, in Phase 2). Changing the order of two lines can change the result completely. - 2
The computer's brain, the CPU (central processing unit), only understands machine code: instructions written as numbers, such as "add the number in box 1 to the number in box 2". Machine code is different for different chips (an Intel laptop and an Apple M-series laptop speak different machine codes) and is almost impossible for humans to write.
- 3
A programming language is a middle ground: precise enough for a machine, readable enough for a person. You write source code in the language, and a translator program turns it into something the machine can run. Java's translator is called
javac(Topic 0.2), and the Java Virtual Machine finishes the job at run time (Topic 0.8). - 4
Most programs follow the shape input → processing → output. Input is the data you start with (a number, a file, a click). Processing is the work (add, sort, search). Output is the result (text on the screen, a saved file). The programs in this course get their input from fixed values written in the code, so they print the same output every time.
- 5
Computers never guess. They do what you wrote, not what you meant. A mistake in a program is called a bug. Some bugs stop the program from being translated at all (the translator shows an error, Topic 0.7). Others let the program run but produce the wrong answer, which is why programmers test their code with examples whose answers they already know.
- 6
Every app you have used, from a game to a bank website, is built from the same few ideas: storing values, doing arithmetic, making decisions, repeating steps, and grouping steps into named pieces. This course teaches each idea in Java, one at a time, and connects them as you go.
Explain it without notes
Explain what a program is to someone who has never used a computer.
Why can't we write programs directly in the language a CPU understands?
What is the difference between an error the compiler shows you and a logic bug?
Describe input, processing and output for a calculator app.
Practice
Write a program that prints your morning routine in four numbered steps.
Write a program that stores the price of one pencil (5) and the number of pencils (7), then prints the total cost.
Write a program that starts with 100 points, loses 30, gains 45, and prints the points after each change.
Trade-offs
- ↔
Writing very small, obvious steps makes a program easy to read and check, but long. Grouping steps into named pieces (methods, Phase 3) keeps it short without hiding what it does.
- ↔
Higher-level languages like Java are much faster to write and safer than machine code, at the cost of a translation step and some control over exactly what the CPU does. For almost all software that trade is worth it.
Done when you can
Done when you can explain what a program is using the recipe analogy.
Done when you can say what a CPU, memory and machine code are.
Done when you can explain why programming languages and translators exist.
Done when you can point to the input, processing and output in a small program.
Done when you can tell a compiler error from a logic bug.