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 strings and other variables inside the parentheses and displays their values on the system output device, which is the computer’s monitor. Here’s an example:

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

This statement causes the following text to be displayed:

Silence affects everyone in the end.

Displaying text on the screen often is called printing, which is what println() stands for—print line. You can use the System.out.println() statement to display text within double quotation marks and also to display variables, as you see later. 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 enables 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 ");
System.out.println ("word.");

These statements cause the following text to be displayed:

She never said another word.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.204.96