Week 14 · 14.3 · Thu, Nov 5 · The Specific Skills + Try Its
Print It: println, print and What the Screen Shows
Today is about exactly what shows up on the screen. It starts with one look back, at where a statement ends. Then you learn the difference between println and print, read text in quotes exactly as written, place empty lines, and trace what each line prints. Those are the four kinds of steps on today's bench.
Where a Statement Ends
Big idea. A semicolon works like the period at the end of a sentence. Each semicolon ends one statement, wherever it sits on the line.
A statement is one complete step for the computer, and in Java a statement ends with a semicolon. To count the statements in main, count its semicolons, not its lines. One line can hold two statements, and each one still runs. The line that names the class, the line that names main and a brace on its own are not statements, so none of them ends with a semicolon.
Worked example. Count the statements in main, then predict what the program prints.
1public class Main 2{ 3 public static void main(String[] args) 4 { 5 System.out.println("Ready"); System.out.println("Set"); 6 System.out.println("Go"); 7 } 8}
Ready Set Go
Many readers count two statements, one for each line. But line 5 holds two semicolons, so main holds three statements. Each one is a println, so each prints its own line.
println and print
Big idea. println is like typing a line and pressing Enter. print is typing without pressing Enter, so the next words land on the same line.
Java has two ways to put text on the screen. System.out.println prints the text in its parentheses and then ends the line, like pressing Enter. The next output starts on a new line. System.out.print prints the text but does not press Enter, so the next output continues right after it, on the same line.
How to predict what a set of print lines shows:
- Go one line at a time, from the top.
- Write exactly what the line prints, right after anything already on the current line.
- If the line is println, end the line, so the next output starts below.
Example 1. Predict what two println lines print.
1System.out.println("Who"); 2System.out.println("Dat");
Show the solution
Who Dat
Each println ends its line, like pressing Enter, so Dat starts on a new line.
Example 2. Predict what two print lines print.
1System.out.print("Who"); 2System.out.print("Dat");
Show the solution
WhoDat
Many readers put Dat on its own line, or add a space. But print never ends the line, so Dat lands right after Who. There is no space between them, because a space only prints if it is inside the quotes, and neither pair of quotes has one.
Example 3. Mix print and println.
1System.out.print("Who "); 2System.out.println("Dat"); 3System.out.println("Nation");
Show the solution
Who Dat Nation
Line 1 prints Who and a space, and the line stays open. Line 2 adds Dat and ends the line. So line 3 starts on the next line down.
Text in Quotes
Big idea. Quotes work like a photocopier: whatever is inside comes out exactly the same. Without quotes, Java acts like a calculator and works out the answer first.
Text inside double quotes is called a string literal. Java prints every character between the quotes exactly as written: spaces, commas and digits too. The quotes themselves never print. Without quotes, Java works out the value first and prints the answer.
Example 4. Predict a line with a comma, a space and digits inside the quotes.
1System.out.println("Saints, 2026");
Show the solution
Saints, 2026
The comma, the space and the digits are all inside the quotes, so all of them print.
Example 5. Compare a line with quotes and a line without them.
1System.out.println("2 + 3"); 2System.out.println(2 + 3);
Show the solution
2 + 3 5
The first line has quotes, so it prints the text 2 + 3 exactly as written. The second line has no quotes, so Java does the math and prints 5.
Empty Lines
Big idea. println() with nothing inside is pressing Enter on its own.
A println with nothing inside its parentheses prints nothing and just ends the line, like pressing Enter on its own. If the line was already empty, that leaves a blank line on the screen.
Example 6. Put an empty line between two words.
1System.out.println("Top"); 2System.out.println(); 3System.out.println("Bottom");
Show the solution
Top Bottom
Line 1 prints Top and ends the line. Line 2 starts on a new, empty line and ends it right away, so one blank line sits between Top and Bottom.
Example 7. Use an empty println after a print.
1System.out.print("Top"); 2System.out.println(); 3System.out.println("Bottom");
Show the solution
Top Bottom
This time line 1 uses print, so the Top line is still open. Line 2 ends that line. Nothing was empty, so no blank line appears.
Tracing What Is Printed
Big idea. For every line, ask one question: does it press Enter?
A trace table for printing has one row for each line that runs. The Output column holds exactly what that line prints. A third column, ends the line?, helps you keep track: print says no, println says yes.
Example 8. Trace these lines, then write the real output.
1System.out.print("Ready"); 2System.out.print(", set"); 3System.out.println(", go."); 4System.out.println("Line two"); 5System.out.println(); 6System.out.print("Done");
Show the solution
line Output ends the line? 1 Ready no 2 , set no 3 , go. yes 4 Line two yes 5 yes 6 Done no
Ready, set, go. Line two Done
Lines 1 and 2 use print, so Ready and , set stay on the first screen line. Line 3 adds , go. and ends that line. Line 4 prints Line two and ends the second line. Line 5 prints nothing and ends the third line, so the third line is blank. Line 6 prints Done on the fourth line.
Quick checks.
- What if line 1 of Example 2 used println? (Then Who and Dat would print on separate lines.) - What does System.out.println("1 + 1"); print? (1 + 1, exactly as written, because it is in quotes.)
Watch for three common mistakes on the bench. First, print does not end the line; only println does. Second, a space prints only if it is inside the quotes. Third, text in quotes prints as written, so "2 + 3" prints 2 + 3, not 5.
Sources: Oracle, Class PrintStream (Java SE 8) and The Java Tutorials: Strings; course repository, Bench Basics B13 job data and the course JDK (2026).
Work each one in your notebook first, then check it.
Try It 1. Which of these is one complete Java statement?
Try It 2. What is printed?
1System.out.print("Red"); 2System.out.print("Beans");
Try It 3. How many lines of output does this print?
1System.out.print("A"); 2System.out.println("B"); 3System.out.println("C");
Try It 4. Which pair of System.out lines prints Who Dat on one line?
Try It 5. What does this line print?
1System.out.println("10 - 4");
Try It 6. What does this line print?
1System.out.println(10 - 4);
Try It 7. Which line prints exactly Go Saints, with one space between the words?
Try It 8. How many empty lines appear between Top and Bottom?
1System.out.println("Top"); 2System.out.println(); 3System.out.println(); 4System.out.println("Bottom");
Try It 9. What is printed?
1System.out.print("Who"); 2System.out.println(); 3System.out.println("Dat");
Try It 10. In a trace table, what goes in the Output column for line 2?
1System.out.print("Go"); 2System.out.println(" team"); 3System.out.println("Win");
Try It 11. Trace these lines the way Example 8 does. Then write the real output.
1System.out.print("Red "); 2System.out.println("Beans"); 3System.out.println(); 4System.out.println("Rice");
Check your answer
Line 1 prints Red and a space and does not end the line. Line 2 prints Beans and ends the first line. Line 3 prints nothing and ends the second line, so it is empty. Line 4 prints Rice. The output is Red Beans, then an empty line, then Rice.