2. Printing Output

2.1. Objectives

  • Practice printing statements and working with escape characters
  • Trace a series of print statements and identify correct output

2.2. Time Goal

  • 10 minutes on this section

2.3. Key Terms

string
Zero to many characters enclosed in starting and ending double quotes " in Java.
newline
A special character sequence that moves text to the beginning of the next line. \n on Mac and Unix based systems and \r\n on windows systems.
escape character
Two characters of source code that represent a single character

2.4. Exercises

Note

Phrases that appear in quotation marks are called strings, because they contain a sequence of “characters” strung together. Characters can be letters, numbers, punctuation marks, symbols, spaces, tabs, etc. Examples of characters include a, 3, and !.

System.out.println is the way that you ask Java to print out a string.

        Q-71: Q-70: The following program segment should print the words to this famous poem
by Emily Dickinson.
   It’s all I have to bring today—
   This, and my heart beside—
   This, and my heart, and all the fields—
   And all the meadows wide—
   Be sure you count—should I forget
   Some one the sum could tell—
   This, and my heart, and all the Bees
   Which in the Clover dwell.
But the blocks have been mixed up. Drag the blocks from the left and put them in the correct order on the right. Click the Check Me button to check your solution.

public class Poem { --- public static void main(String[] args) { --- System.out.println("It’s all I have to bring today—"); --- System.out.println("This, and my heart beside—"); --- System.out.println("This, and my heart, and all the fields—"); --- System.out.println("And all the meadows wide—"); --- System.out.println("Be sure you count—should I forget"); --- System.out.println("Some one the sum could tell—"); --- System.out.println("This, and my heart, and all the Bees"); --- System.out.println("Which in the Clover dwell."); --- } --- }

Note

Strings & Escape sequences

System.out.println appends a special character, called a newline, that moves to the beginning of the next line. If you don’t want a newline at the end, you can use print instead of println. It’s possible to display multiple lines of output with only one line of code. You just have to tell Java where to put the line breaks.

The following output is two lines, each ending with a newline character:

Hello!
How are you doing?

Each \n is an escape sequence, or two characters of source code that represent a single character. (The backslash allows you to “escape” the string to write special characters.) Notice there is no space between \n and How. If you add a space there, there will be a space at the beginning of the second line.

Common escape sequences

\n newline
\t tab
\" double quote
\\ backslash

Java has a total of eight escape sequences, and the four most commonly used ones are listed in the table above. For example, to write quotation marks inside of strings, you need to escape them with a backslash.

System.out.println("She said \"Hello!\" to me.");

The result is:

She said "Hello!" to me.

    Q-72: Select all of the blocks of code below that would produce the following output if added to the main method:

    "Hi"!
    How are you?
    

    You may use this coding window to help evaluate the output for each sequence.

  • System.out.println("\"Hi\"!")
    System.out.println("How are you?");
    
  • System.out.print("\"Hi\"!
    How are you?
    ");
    
  • System.out.println("\"Hi\"!");
    System.out.println("How are you?");
    
  • System.out.println("\"Hi\"!");
    System.out.println("How "+ "are "+ "you?");
    
  • System.out.println("\"Hi\"!");
    System.out.print("How");
    System.out.print("are");
    System.out.print("you?");
    
    Q-73: Which is the escape character used in Java?
  • ;
  • Incorrect--a semicolon usually ends a statement in code, but has no effect when priting.
  • .
  • Incorrect--periods have no effect when printing. They just look like periods.
  • /
  • Incorrect--forward slashes look no different in your code than when printed.
  • \
  • Correct, this character is used with another character to create an escape sequence
  • |
  • Incorrect--a vertical bar looks no different in your code than when printed.
    Match the name of an escape sequence with its characters
  • newline
  • \n
  • tab
  • \t
  • double quote
  • \"
  • backslash
  • \\
Next Section - 3. Debugging