✕ Close

Week 14 · 14.3 · Thu, Nov 5 · The Bigger Picture: ACT-style reading

Hello, World: Why the First Program Prints

1 This section explains why the first program in so many programming books does nothing but print a short greeting, and how a Java program decides exactly what appears on the screen and where each word lands.

A Greeting from Bell Labs

2 In the early 1970s, Brian Kernighan worked at Bell Laboratories in Murray Hill, New Jersey, where researchers were building new programming languages. He wrote a tutorial for a language called B, and one of its examples printed the words "hello, world," followed by an exclamation point. The example was not really about greetings. It was there to show how a program could store pieces of text and put them together on the screen.

3 Around 1974, Kernighan wrote a second tutorial, "Programming in C," for a newer language named C. Its first program had a single job, which was to print "hello, world." In 1978, Kernighan and Dennis Ritchie, who wrote the reference manual for C, published a book called The C Programming Language. The book also used a program that prints "hello, world" as the smallest C program that works. Since then, many books and tutorials have followed the same habit, and Oracle's Java Tutorials start with an application that prints a Hello World greeting.

Why the Screen Comes First

4 A program that only prints a greeting may seem too simple to be worth writing. Getting it to run, however, proves that every step before it works. The code has to be typed correctly, the language's tools have to turn it into something the computer can run, and the computer has to run it. If any of those steps fails, the greeting never appears, so the programmer has a clear place to start looking. For that reason, programmers often use a "Hello, World" program to check that their tools are set up correctly.

5 Output, the text a program puts on the screen, is also how a person sees what a program did. A program can store numbers, compare them and make choices, but nothing appears unless a line of code puts it on the screen. When the output is wrong, the person reading it can tell that something in the program went wrong, too. That is why reading a program and predicting exactly what it prints is one of the first skills a programmer practices.

What Java Prints

6 Java has two common statements for printing. The statement System.out.println prints its text and then ends the line, so whatever is printed next starts on a new line. The statement System.out.print prints its text but does not end the line, so whatever comes next continues right after it, on the same line. Oracle's documentation says that println behaves as though it calls print and then ends the line.

7 Text inside double quotes is called a string literal. Java prints every character between the quotes exactly as written, including spaces and punctuation, but it does not print the quotes themselves. A space at the start or end of the text counts, too, which is why "Hello " with a space prints differently from "Hello" without one. A number without quotes is different. The statement System.out.println(2 + 3) works out the math and prints 5, while System.out.println("2 + 3") prints the text 2 + 3, because the quotes make it a string literal.

8 The four lines below print the same two words twice, and Figure 1 shows the same two pairs beside what the screen shows. The first pair uses print and then println, and the second pair uses println twice.

1System.out.print("Hello ");
2System.out.println("World");
3System.out.println("Hello");
4System.out.println("World");
Hello World
Hello
World

9 The first pair puts both words on one line, because print does not end the line. The space after Hello is inside the quotes, so it is printed, too. The second pair puts the words on two lines, because println ends each line, so World starts on a new line. Today's question asks how the same two words could end up on one line or on two, and the choice between print and println is the answer. On today's bench, every prediction is checked exactly, so each space and each line break counts.

Summary

10 The habit of making a first program print "hello, world" comes from Brian Kernighan's tutorials at Bell Labs in the 1970s and from the 1978 book he wrote with Dennis Ritchie. A program that prints a greeting proves that the tools work, and printing is how a person sees what any program did. In Java, println prints text and ends the line, print prints text and stays on the same line, and a string literal prints exactly as it is written between its quotes.

Sources: Kernighan, A Tutorial Introduction to the Language B (Bell Laboratories); Kernighan, Programming in C: A Tutorial (Bell Laboratories); Wikipedia, "Hello, World" program and The C Programming Language; Oracle, The Java Tutorials: A Closer Look at the Hello World Application and Strings; Oracle, Class PrintStream (Java SE 8).

Figure 1. Kernighan's tutorials at Bell Labs in the 1970s made printing "hello, world" the first program, and in Java, print and println decide whether two words land on one line or on two.
ACT-style reading · 10 questions

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, the "hello, world" example in Kernighan's B tutorial was there to show:

3. Words in context

As it is used in paragraph 4 ("Getting it to run, however, proves that every step before it works"), the word proves most nearly means:

4. Purpose

The sentence "The code has to be typed correctly, the language's tools have to turn it into something the computer can run, and the computer has to run it" (paragraph 4) mainly serves to:

5. Inference

Based on paragraph 7, what does System.out.println("2 + 3") print?

6. Sequence

According to the passage, which of these happened FIRST?

7. Cause and effect

In the four lines in paragraph 8, Hello and World end up on the same line in the first pair because:

8. Detail

According to paragraph 6, Oracle's documentation says that println behaves as though it:

9. Author's claim

Which statement from the passage best answers today's question about how two words could end up on one line or on two?

10. Figure

According to Figure 1, which pair of Java lines puts Hello and World on two separate lines of the screen?