ptg7068951
WHAT YOU’LL LEARN IN
THIS HOUR:
.Using strings to store text
.Displaying strings in a
program
.Including special charac-
ters in a string
.Pasting two strings
together
.Including variables in a
string
.Comparing two strings
.Determining the length of
a string
In the film The Piano, Holly Hunter portrays Ada, a young Scottish woman
who is mute and can express herself only by playing her piano.
Like Ada, your computer programs are capable of quietly doing their work
and never stopping for a chat—or piano recital—with humans. But if The
Pianoteaches us anything, it’s that communication ranks up there with food,
water, and shelter as essential needs. (It also teaches us that the actor Harvey
Keitel has a lot of body confidence, but that’s a matter for another book.)
Java programs use strings as the primary means to communicate with
users. Stringsare collections of text—letters, numbers, punctuation, and
other characters. During this hour, you learn all about working with
strings in your Java programs.
Storing Text in Strings
Strings store text and present it to users. The most basic element of a string
is a character. Acharacteris a single letter, number, punctuation mark, or
other symbol.
In Java programs, a character is one of the types of information that can be
stored in a variable. Character variables are created with the chartype in a
statement such as the following:
charkeyPressed;
This statement creates a variable named keyPressedthat can store a char-
acter. When you create character variables, you can set them up with an
initial value, as in the following:
charquitKey = ‘@’;
HOUR 6
Using Strings to Communicate