Week 14 · 14.2 · Wed, Nov 4 · The Bigger Picture: ACT-style reading
Start to Stop: From Ada Lovelace's Steps to a Java Program
1 This section explains how a list of steps written in 1843 shows the idea behind every program, and how a Java program decides where to start, which line runs next and when to stop.
A List of Steps in Order
2 Ada Lovelace was born in 1815, the daughter of the poet Lord Byron. In 1843 she published an English translation of an article about the Analytical Engine, a calculating machine that the inventor Charles Babbage had designed. She added notes of her own, and Babbage later wrote that her notes were about three times as long as the article itself.
3 According to the Computer History Museum, the notes included the first published description of a stepwise sequence of operations for solving certain mathematical problems. The last note, Note G, laid out a plan for computing a series of numbers called the Bernoulli numbers. The plan numbered its operations, used twenty-five of them and showed the successive changes the machine would make, one after another. Many people today consider this plan the first computer program.
4 Babbage had first offered to work out the Bernoulli plan himself. Lovelace sent his version back to him, he wrote, "having detected a grave mistake which I had made in the process." A machine would carry out a plan exactly as written, so a plan was correct only when every step was correct and came in its place.
Where a Java Program Starts
5 A Java program is also a list of steps, but a Java file holds more than steps. Line 1 usually names a class, and braces mark where each part begins and ends. According to the Java Language Specification, the Java Virtual Machine starts running a program by calling the method named main. This method is called the main method, and every Java application must have one. For that reason, the first step that runs is the first statement inside the main method, not line 1 of the file. Figure 1 shows the path from Lovelace's numbered steps, to a Java file whose start is marked at main, to three statements numbered in the order they run.
6 The method main does not have to be at the top of the file. A class can hold other methods above it, and Java still begins at main, because it looks for main by name. A method that main never calls is never run at all, even though its lines sit higher in the file.
One Statement at a Time
7 Each step inside main is a statement, such as a line that stores a value or a line that prints. In the short programs in this class, each statement ends with a semicolon. The Java Language Specification says that the statements in a block run "in order from first to last." This order is called a sequence: one statement finishes before the next one starts. The program below prints three words, and they always appear in the same order, because the three statements run one after another.
1public class Main 2{ 3 public static void main(String[] args) 4 { 5 System.out.println("Ready"); 6 System.out.println("Set"); 7 System.out.println("Go"); 8 } 9}
Ready Set Go
8 Because statements run in order, moving a line changes what a program does. If a program stores 4 in x, adds 1 to x and then doubles x, it ends with 10. If it doubles first and adds 1 second, it ends with 9. Each statement uses the values that the statements above it have already stored.
Where It Stops
9 After the last statement inside main runs, the program reaches the closing brace of main. Nothing tells it to go back, so it does not repeat. The program stops there, even when main called another method along the way, because a called method runs and then comes back to main. The brace below it closes the class and is not a step.
10 Today's question asks how a Java program decides which line runs first, which runs next and when it stops. The answer has three parts: start at main, run one statement at a time from top to bottom, and stop at the end of main. On today's bench, the lines of a program are clicked in the order they run, including programs whose main method is not at the top.
Summary
11 Lovelace's notes of 1843 described a numbered sequence of steps for a machine, and the plan worked only if each step was right and in its place. A Java program follows the same idea. It starts at the method main, wherever main is in the file, runs its statements in sequence from first to last and stops when main's last statement is done.
Sources: O'Connor and Robertson, Augusta Ada King, Countess of Lovelace (MacTutor History of Mathematics, University of St Andrews); Computer History Museum, The Babbage Engine: Ada Lovelace; Menabrea, Sketch of the Analytical Engine, with notes by the translator, Ada Augusta, Countess of Lovelace (1843; Fourmilab text); Oracle, The Java Language Specification, Java SE 21 Edition, §12.1 and §14.2.
Read the passage on the left. Answer each question by clicking a choice: the right answer turns green, and a wrong one tells you why. Questions point to paragraph numbers, as on the ACT.
1. Main idea
The main purpose of the passage is to:
2. Detail
According to paragraph 2, what did Lovelace publish in 1843?
3. Words in context
As it is used in paragraph 4 ("having detected a grave mistake"), the word grave most nearly means:
4. Purpose
The story of Babbage's mistake in paragraph 4 mainly serves to:
5. Inference
Based on paragraphs 5 and 6, a Java file has a method named other at the top and the method main below it. Nothing calls other. Which statement runs first?
6. Sequence
According to paragraph 8, a program stores 4 in x, doubles x and then adds 1 to x. What does x hold at the end?
7. Cause and effect
According to paragraph 5, the first step that runs is the first statement inside main because:
8. Detail
According to paragraph 9, what happens after the last statement inside main runs?
9. Author's claim
Which statement from the passage best expresses how Lovelace's 1843 plan and a Java program are alike?
10. Figure
According to the third panel of Figure 1, which statement runs last, just before the program reaches main's closing brace and stops?