Skip to main content
Logo image

Section 2.2 Text output

A lot of what happens when a computer program runs is invisible, happening only within the computer’s memory. But ultimately we need some way to convey information to humans outside the computer. If running the program has no effect on the world outside the computer, what was the point of running it? These days computers can communicate with the outside world in all sorts of ways: sound, graphics, animated videos, even physical robots moving around the world. But one of the simplest ways for a program to communicate with us is to emit textual output to the screen.
In this section we will learn about how Java represents text and how to print values to the screen allowing us to write programs that actually do something.

Subsection 2.2.1 println and print

Java has two different methods to print output to the screen which we saw in passing in the Introduction.
  • System.out.println(value) prints the the textual representation of value followed by a new line (println is an abbreviation for “print line”.)
  • System.out.print(value) prints the textual representation of value without advancing to the next line
The value between the parentheses can be any Java expression which just means some bit of code that produces a value. One simple kind of expression is a literal value which is a way of representing a value directly—or literally—in our code. For instance 42 is a literal expression representing the int value 42. When we print a value with print or println, Java converts the value to its textual representation and then emits that to the screen. So we could write System.out.print(42) to print the text 42. Another trivial kind of expression is a variable, whose value is whatever value was last assigned to it.

Subsection 2.2.2 Strings

In order to print out a value Java has to convert it to text. In Java, text is represented with a data type called a String. Why are they called "strings"? Who knows? Java didn’t make it up; computer scientists have been using “string” to refer to sequences of items since at least the 1940s. Think “string of pearls” or “string of ponies” but in this case a “string of characters”, where “character” is what computer scientists call letters, numbers, punctuation, spaces, etc.
If we wanted to store any text, from a person’s name to the contents of a novel, in a Java program, we would represent it as a String. As another data type, like int and double, we can declare String variables. We’ll learn a lot more about the String data type in Section 7.1 Manipulating strings but for now we just need to know we can write string literals by enclosing the text we want in quotation marks: "".
For instance "Hi there!" is a string literal whose value is the text: Hi there!. The quotation marks are not part of the value; they are the syntax for writing the string literal. Thus the code System.out.println("Hi there!"); prints the text Hi there! on the screen, without the quotation marks, and then advances to the next line.

Activity 2.2.1.

Run this code to see the output below it. How would you change it to print the ! on the same line as Hi there keeping all 3 print statements?

Subsection 2.2.3 String concatenation

Another handy String feature for printing output is the string concatenation operator, written with +, the same as arithmetic addition. When we write a + expression where one of the operands is a String, it produces a new String by smooshing together the String with the String representation of the other value. For instance if name and age were variables containing someone’s name and age we could write:
System.out.println("Name: " + name + "; Age: " + age);
to get output like:
Name: Sally Sue; Age: 17
Note that there’s an important difference between a variable in a string concatenation expression and the name of the variable in quotes "". The latter is just a String whose text happens to be the same as the name of the variable while the former will produce the String representation of the variable’s value. A common mistake is to write something like System.out.println("x") when we meant to write System.out.println(x) On the other hand, a really good way to write that would be: System.out.println("x: " + x) which would emit the value of x labeled with the name of the variable.
Also if we want to make readable output with spaces between the concatenated pieces variables, we need to the spaces inside the quotes of some String literal. Without spaces, we will get output like HiJose instead of Hi Jose.

Activity 2.2.2.

Run the following code to see what is printed. Then, change the values and run it again. Try adding quotes to variables and removing spaces in the print statements to see what happens.

Subsection 2.2.4 String escapes

What if we wanted to print out a double quote " character? Since " has a special meaning within Java’s string literals, it might seem there’d be no way to include it within a string. However, Java provides a way to write string literals that contain double quotes using a mechanism called escaping. To “escape” the normal syntax of a string literal, where a quotation mark would indicate the end of the string, we put a backslash (\) in front of the quotation mark which tells Java to include the following quotation mark in the string value rather than treating it as the end of the literal.
This is called a backslash escape sequence. Since backslash is used this way, that also makes it a special character, so if we want to include a backslash in a string literal, we need to escape it with another backslash! There are a handful of other backslash escape sequences. The only other one the College Board wants you to know about is \n which will be translated to a newline character, which will break the text across lines.

Note 2.2.1.

Unfortunately, different operating systems use different characters to represent the end of a line. println actually uses whatever is appropriate on the computer the program is running on. On MacOS and Linux that will be the same character as we can write in a string literal with \n. On Windows, however, it will be the two character sequence that would be written \r\n. To make our code run the same everywhere, we’d have to use only println when we want a new line. The College Board does want you to know about the \n escape sequence, however.

Activity 2.2.3.

Here are the escape sequences that may be used in the AP course.

Subsection 2.2.5 Input

The flip side of emitting output to the user is getting input from the user. Just as there are lots of ways for computers to generate output, modern programs can accept all kinds of input: UI gestures such as the user clicking on a button, audio input from the computer’s microphone, visual input from a camera, etc. But as with output the simplest to deal with is textual input. In Chapter 12 text-files we’ll look at how to read input from files saved on the computer. But sometimes it’s useful to be able to get input typed by the user as a program runs.
The AP curriculum doesn’t require you to know any particular way of dealing with user input but does mention the Scanner class that comes with Java as one way. And knowing how to use Scanner will let us write programs whose behavior can change depending on their input.
The code below uses the Scanner class to read a name the user types in and then says hello. It will have different results depending on what the user types. Try it by typing in your name in the box below the code before you click “Run”. Try again with a friend’s name. The code works for any name.

Activity 2.2.4. Program with user input.

The code below will say hello to anyone who types in their name. Type in your name in the box below the code and then click on “Run”. Try again with a friend’s name.
Although you will not be tested in the AP CSA exam on using the Java input from the keyboard, learning how to do input in Java is very useful and fun. For more information on using the Scanner class, go to https://www.w3schools.com/java/java_user_input.asp, and for the newer Console class, https://howtodoinjava.com/java-examples/console-input- output/. We are limited with the one way communication with the Java server in this Runestone ebook, but in most IDEs, the input/output would be more interactive. You can try this Scanner input example in JuiceMind (click on Create Starter Code after login with a Google account) or Scanner input example in Replit using the Scanner class and Console input example using the Console class. We will also learn how to use Scanner with input files in a later unit.

Subsection 2.2.6 Coding Challenge: Mad Libs

Have you ever played MAD LIBS? In this game, you first choose a bunch of words following clues like give me a color or a plural noun, without looking at the story, and then those words are filled into the story to make it sound very wacky! Fill in the variables below with silly words, and then run to see the wacky story.
Then, working in pairs, come up with another silly story that uses at least 5 new String variables. When you’re done, try another team’s mad libs code. For more advanced programming, you could create this program in a Java IDE that can do input using the Scanner class to read in input into the variables.

Project 2.2.5. Mad Libs.

Replace the text “Replace” below with silly words following the description in the variable names (for example, “cats” for a plural noun, “blue” for a color, etc.) to create a silly poem. Run the code to see the poem. Then, create your own silly story using 5 more String variables.

Subsection 2.2.7 Summary

  • (AP 1.3.A.1) System.out.print and System.out.println are Java output statements that display information on the computer screen. System.out.println moves the cursor to a new line after the information has been displayed, while System.out.print does not.
  • (AP 1.3.B.1) A literal is the code representation of a fixed value, which can be a string or a numerical value.
  • (AP 1.3.B.2) A string literal is a sequence of zero to many characters enclosed in starting and ending double quotes.
  • (AP 1.3.B.3) Escape sequences are special sequences of characters that can be included in a string. They start with a \ and have a special meaning in Java. Escape sequences used in this course include double quote " , backslash \ , and newline n.
  • (AP 1.4.B.1) Input can come in a variety of forms, such as tactile, audio, visual, or text. The Scanner class is one way to obtain text input from the keyboard, although input from the keyboard will not be on the AP exam.

Subsection 2.2.8 AP Practice

Activity 2.2.6.

Consider the following code segment.
System.out.print("Java is ");
System.out.println("fun ");
System.out.print("and cool!");
What is printed as a result of executing the code segment?
  • Java is fun and cool!
    
  • Notice the println in line 2.
  • Java isfun
    and cool!
    
  • Notice the space after is in the first line.
  • Java is
    fun
    and cool!
    
  • Notice that the first line is a print, not println.
  • Java is fun
    and cool!
    
  • Correct! Looks like you paid attention to which lines used print and which ones used println.
You have attempted of activities on this page.