Using Java Strings to Communicate
See all Sams Teach Yourself on InformIT Programming Tutorials.
In the film The Piano, Holly Hunter portrays Ada, a young Scottish woman who marries badly. A mute since the age of six, Ada can only express herself fully by playing her prized possession, a piano.
Like Ada, your computer programs are capable of quietly doing their work and never stopping for a chator piano recitalwith humans. However, if The Piano teaches us anything, it is that communication ranks up there with food, water, and shelter as an essential need. (It also teaches us that Harvey Keitel has a lot of body confidence, but that's a matter for another book.)
Java programs don't have access to a piano. They use strings as the primary means to communicate with users. Strings are collections of textletters, numbers, punctuation, and other characters. During this hour, you will learn all about working with strings in your Java programs. The following topics will be covered:
Using strings to store text
Displaying strings in a program
Including special characters in a string
Pasting two strings together
Including variables in a string
Some uses for strings
Comparing two strings
Determining the length of a string
Changing a string to upper- or lowercase
Storing Text in Strings
Strings are a common feature in computer programming because they provide a way to store text and present it to users. The most basic element of a string is a character. A character is 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 char type in a statement such as the following:
This statement creates a variable named keyPressed that can store a character. When you create character variables, you can set them up with an initial value, as in the following:
char quitKey = '@';
Note that the value of the character must be surrounded by single quotation marks. If it isn't, the Java compiler will respond with an error when the program is compiled.
A string is a collection of characters. You can set up a variable to hold a string value by using the String text and the name of the variable, as in the following statement:
String fullName = "Ada McGrath Stewart";
This statement creates a string variable called fullName and stores 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 will not be included in the string itself.
Unlike the other types of variables you have usedint, float, char, boolean, and so onthe name of the String type is capitalized.
The reason for this is that strings are somewhat different than the other variable types in Java. Strings are a special resource called objects, and the types of all objects are capitalized. You'll be learning 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, String is capitalized when strings are used in a statement.