Storing Text in Strings

Strings 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:

char keyPressed;

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 = '@';

The value of the character must be surrounded by single quotation marks.

A string is a collection of characters. You can set up a variable to hold a string value by following String with the name of the variable, as in this statement:

String fullName = "Ada McGrath Stewart";

This statement creates a string variable called fullName containing 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 String type 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, String is capitalized.

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

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