Chapter 1 · Foundations
Inside the computer
Step 1 of 17 · Concept
A computer, simplified
Every computer has three main parts:
| part | what it does | keeps things when off? |
|---|---|---|
| CPU | carries out instructions, one at a time, very fast | no |
| memory | holds the programs that are running, and their values | no |
| storage | holds files, such as Hello.java | yes |
Step 2 of 17 · Quiz
Where is it kept?
You close the browser and come back tomorrow. Hello.java is still there. Where was it kept?
Step 3 of 17 · Concept
Memory is numbered boxes
- Memory is a long row of tiny boxes called bytes. A byte holds a number from 0 to 255.
- Each box has a number, its address. The CPU finds a box by its address.
- Bigger values use several boxes in a row. A Java
intuses 4.
Step 4 of 17 · Quiz
When the power goes
The computer turns off. What happens to the values in memory?
Step 5 of 17 · Concept
From your file to a running program
javacreadsHello.javafrom storage and writesHello.class: bytecode, instructions for the Java virtual machine.javastarts the JVM, the Java virtual machine, in memory. The JVM loadsHello.class.- The JVM carries out the bytecode with the CPU, starting at
main.
Step 6 of 17 · Quiz
What is in Hello.class?
What does Hello.class hold?
Step 7 of 17 · Concept
One instruction at a time
- The JVM keeps a program counter: a marker on the next instruction.
- Again and again, it reads that instruction, carries it out, and moves the marker on.
- In this course we put the marker on a line of code. Really, one line becomes several bytecode instructions.
Step 8 of 17 · Trace
Watch the program counter
Press Next to run one line. Watch the marker and the output.
public class Steps {
public static void main(String[] args) {
System.out.println("Ready");
System.out.println("Set");
System.out.println("Go");
}
}- Line 3 is next. Nothing is printed yet.
- Line 3 ran and printed
Ready. Line 4 is next. - Line 4 ran and printed
Set. Line 5 is next. - Line 5 ran and printed
Go.mainis over, so the program ends.
Step 9 of 17 · Try it
Try it: put it in order
The lines run from the top, one at a time. Move them so the program prints:
3
2
1
Go!
Cut a whole line and paste it where it belongs. Keep every line inside main.
Step 10 of 17 · Concept
Input and output
| goes from … to … | in Java | |
|---|---|---|
| output | the program to the screen | System.out.println, which you know |
| input | the keyboard to the program | Scanner, in lesson 6 |
| files | the program to storage, and back | Files, in lesson 19 |
Step 11 of 17 · Concept
The stack: a frame for each method
int age = 15;
double price = 2.5;
- When
mainstarts, the JVM puts a frame for it on the stack, a part of memory for running methods. - Each value
mainkeeps is a box in its frame. You write these in lesson 3. - When
mainends, its frame is removed.
Step 12 of 17 · Concept
The heap: where objects live
int age = 15;
String name = "Ada";
int[] temps = {12, 15, 9};
- A number sits right in its box.
- Text, arrays and objects live in another part of memory, the heap. Their box holds a reference, drawn as an arrow.
Step 13 of 17 · Quiz
Where does it live?
String name = "Ada"; Where are the box name and the text "Ada"?
Step 14 of 17 · Trace
Calls stack up
main calls the method greet. Press Next and watch the stack.
public class Calls {
static void greet() {
System.out.println("Hi");
}
public static void main(String[] args) {
System.out.println("Start");
greet();
System.out.println("End");
}
}mainruns: its frame is on the stack. Line 7 is next.Startis printed. Line 8 callsgreet.- A frame for
greetgoes on top ofmain's. Line 3 is next. greetprintedHiand ended: its frame is gone. Back inmain, line 9 is next.Endis printed.mainends, and the stack is empty.
Step 15 of 17 · Quiz
When a method ends
greet has finished. What happened to its frame?
Step 16 of 17 · Check yourself
Check yourself: the life of a program
This program prints the five things that happen when you press Run, in the wrong order. Move the lines so they print in the order they happen.
Storage comes first: nothing can run before javac has read your file.
Step 17 of 17 · Recap
Recap
- A computer has a CPU, memory and storage. Only storage keeps things when it is off.
javacturns your text into bytecode. The JVM loads it into memory and runs it, one instruction at a time.- Each running method has a frame on the stack. It goes when the method ends.
- Text, arrays and objects live on the heap. A box holds a reference to them.
Next: lesson 3 gives your values names: variables, the boxes in a frame.
Review
5 questions from this lesson, in a new order each time. Answer from memory: 3 of 5 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
public class Countdown {
public static void main(String[] args) {
System.out.println("1");
System.out.println("Go!");
System.out.println("3");
System.out.println("2");
}
}
public class Life {
public static void main(String[] args) {
System.out.println("The JVM loads Hello.class");
System.out.println("javac writes Hello.class");
System.out.println("main ends; its frame is removed");
System.out.println("javac reads Hello.java");
System.out.println("The JVM runs main's instructions");
}
}