Printing Output in Java
A Java program is defined within a public class.
Predicting What a First Program Prints
A first Java program can display the message Hello World! on standard output. Standard output is the place where a program's visible text appears, and in this article it means the console. Before learning any details, it helps to look at a complete program and ask a simple question: where in the program does the visible message come from?
What do you think happens?
Given this program, what text appears on the console when it runs?
Reveal answer
Hello World!
The text in quotation marks inside the System.out.println statement is sent to standard output, so it is what the console displays.
Hello World!The goal of this article is not to memorize every word of this program at once. Instead, you will inspect its structure, identify the command that displays the message, and follow the program from its starting point to its output.
The Public Class That Holds the Program
A Java program is defined within a public class. In the example above, the class is named HelloWorld. The class is the container for the program's code, and everything the program does is written inside its braces.
Read the program from the outside in. The outer layer is the public class. Inside that class sits the main method. Inside main sits the statement that prints the message. This nesting is the path you will follow whenever you need to find out why a Java program behaves the way it does.
The main Method as the Entry Point
The main method provides the program's entry point. An entry point is the place where execution begins. When you run the program, Java starts at main, and the statements written inside main are carried out in order. Code that sits elsewhere in the class does not run just because it is there; it runs only when the program's path reaches it.
Printing Text with System.out.println
The println statement displays Hello World! on standard output. In the example, the text is written as a string literal, meaning the exact characters placed between double quotation marks. System.out.println takes that text and sends it to standard output, and the console shows it on a new line.
Welcome to Java
Output is printed one line at a timeChanging the printed message
A program prints Hello World! and you want it to print Hello Java! instead. Which part of the program must change?
- 1Locate the output command
The visible text is produced by the
System.out.printlnstatement, so that is the line to examine. - 2Identify the text
The text is the string literal inside the parentheses and quotation marks, which is currently Hello World!.
- 3Replace only the literal
Change the characters inside the quotation marks to Hello Java! and leave the class and main method untouched.
The statement becomes System.out.println("Hello Java!"); and the console displays Hello Java!.
Tracing Where the Output Is Produced
A trace follows the program step by step, from its starting point to the point where the output appears. For a simple program, the trace is short: the run begins at main, the statements inside main run in order, and the println statement is the one that produces the visible text.
Notice what the trace reveals. The class and the main method organize the program, but the visible message comes from one specific command. When the output is not what you expected, separate the two questions: first check the path from the class to main, and then check the println statement that produces the text.
Comments That the Compiler Ignores
Comments document Java code and are ignored by the compiler. A comment is text written to explain the code to a human reader. Because the compiler skips it, adding or removing a comment does not change what the program prints or how it runs.
Comments do not printMistakes Beginners Make When Reading Output Code
Common mistake Looking for the output in the class declaration
What it looks likepublic class HelloWorld { ... } is read as if it were the line that prints text.Why it failsThe class is the container for the program. It does not itself display anything.
Do this insteadFollow the path from the class to main, then find the System.out.println statement that produces the visible text.
Common mistake Expecting a comment to produce output
What it looks likeA line beginning with // is assumed to print its words to the console.
Why it failsComments are ignored by the compiler, so they never run and never print.
Do this insteadOnly the System.out.println statement sends text to standard output.
Common mistake Editing the class instead of the printed text
What it looks likeRenaming the class when the goal was to change the message shown on the console.
Why it failsThe message comes from the string literal inside println, not from the class name.
Do this insteadChange the characters between the quotation marks inside the println statement.
Practice Tracing a Program
Read this program and identify the public class, the main method, and the statement that produces the output. Then state exactly what text appears on the console.
public class Trace {
public static void main(String[] args) {
// print the first line
System.out.println("Step one");
}
}Hints
- Start at the main method, since that is the entry point.
- Comments are ignored by the compiler, so skip the line that begins with two slashes.
- Only the text inside the println statement reaches standard output.
Key Takeaways
- A Java program is defined within a public class, which acts as the container for the program's code.
- The main method provides the entry point, so execution begins there and follows the statements written inside it.
System.out.printlndisplays its text, such as Hello World!, on standard output, and that statement is the place where the visible message is produced.- Comments document Java code for human readers and are ignored by the compiler, so they never change the printed output.
- When output is unexpected, trace the path from the class to the main method and then to the println statement.