Displaying Strings in Programs

The most basic way to display a string in a Java program is with the System.out.println() statement. This statement takes any strings and other variables inside the parentheses and displays their values. The following statement displays a line of text to the system output device, which is the computer's monitor:

System.out.println("Silence affects everyone in the end.");

The preceding statement would cause the following text to be displayed:

Silence affects everyone in the end.

Displaying a line of text on the screen is often called printing, which is what println() stands for—"print this line." You can use the System.out.println() statement to display text within double quotation marks and also to display variables, as you will see. Put all the material you want to be displayed within the parentheses. Another way to display text is to call System.out.print(). This statement displays strings and other variables inside the parentheses, but unlike System.out.println(), it allows subsequent statements to display text on the same line. You can use System.out.print() several times in a row to display several things on the same line, as in this example:

System.out.print("She ");
System.out.print("never ");
System.out.print("said ");
System.out.print("another ");

These statements cause the following text to be displayed:

She never said another word.