ptg7068951
66 HOUR 6:Using Strings to Communicate
The value of the character must be surrounded by single quotation marks.
A stringis a collection of characters. You can set up a variable to hold a
string value by following Stringwith the name of the variable, as in this
statement:
String fullName = “Ada McGrath Stewart”;
This statementcreates a string variable called fullNamecontaining the text
“Ada McGrath Stewart” in it, which is the full name of Hunter’s pianist. A
string is denoted with double quotation marks around the text in a Java
statement. These quotation marks are not included in the string itself.
Unlike the other types of variables you have used—int, float, char,
boolean, and so on—the name of the Stringtype is capitalized.
Strings are a special kind of information called objects, and the types of all
objects are capitalized in Java. You learn about objects during Hour 10,
“Creating Your First Object.” The important thing to note during this hour
is that strings are different than the other variable types, and because of
this difference, Stringis capitalized.
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 dis-
play variables, as you see later. Put all the material you want to be dis-
played 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.