Strings

From CompSciWiki
Revision as of 15:20, 20 March 2007 by Umhieb34 (Talk | contribs)

Jump to: navigation, search

This section will introduce you to the concept of a String, and how to create and use them. More advanced topics on mutability and escape sequences are presented for those interested in exploring Java Strings further.

What are Strings

String diagram.png

Strings are variables that contain a list of characters. We can represent things such as words or sentences of human language, text from a file, or input from the user's keyboard as Strings. To the computer, a String is simply a consecutive list of characters in memory (this is actually an array, more on these later) as the diagram to the right shows.

Creating Strings

There are many ways we can create Strings in Java, the easiest of which is to simply write the desired contents of the String into a program. When Strings are created in this fashion, the resulting String is known as a String literal. To create a String literal in Java, use double-quotes (") around the characters that you would like to include, and simply write them into your code.

Here are some examples of Strings that may occur within your code:

"This is a String"
"ABC123"
"100100100"
"    "
"Q"

To use a string you have written, simply place it within an expression or assign it to a variable for later use. To create a String variable, simply declare a variable of type String and assign the literal to it.

String address = "66 University Crescent";

Now, address is available and contains the String 66 University Crescent. Strings can also occur as part of an expression, most often to add something to them.

String Expressions

Sometimes when creating a String literal you want to display the contents of a variable, but don't know what the value of the variable will be until after the program has started. In these cases, you can simply append the variables to the String literal using the + operator.

For example, if we have a number that we want to include within a String, we can write it as follows:

  double kilometersToSchool = 14.5;
  String message = "The University is " + total + " kilometers away!";

The second line of code above can be read as 'add (or append) the double total to the end of the String The University is , and then append kilometers away! to the end of that result. When this code is executed, the variable message will contain the text The University is 14.5 kilometers away!.

Note that when creating a String expression, a String must come first before any + operators, or else it will not work. For example, you might think the following code creates the string 17 ducks are in the pond:

int ducks = 17;
String message = ducks + " ducks are in the pond";

But actually, Java doesn't know how to add a String to an int (but it does know how to add an int to a String!)

Special Cases

These are two special cases to consider when dealing with Strings. First, when we want to include a double-quote within a String, we cannot simply place it into a String literal, because Java will confuse it with the two double-quotes occuring at either end of the String. However, we can include a double-quote inside a String by preceding it with a backslash. The second special case is that of double-quotes occuring without any characters in between. In this case, the result is an empty String (known often as the empty string). The following examples demonstrate these special cases:

"\"That's the beauty of it, it doesn't do anything!\" - anonymous"
"" (the empty string)

When your String is read by the computer, the \" combination will be replaced with a double-quote character, ". So the first String in the example above would contain "That's the beauty of it, it doesn't do anything!" - anonymous

Length

Strings are known as having a particular length. The length of a String is simply the count of the number of characters that occurs within it. This count is represented as an integer, and can be obtained via the Calling_Methods#String_Methods method. Knowing the length is useful because you might want to restrict user input based on the number of characters they type, or only process Strings greater than a particular length.

String literals can range from 0 to 216 (65536) characters, though Strings obtained from other sources, such as files or user input, have no practical limit on length. Also note that the empty String, "", has a length of zero since it contains no characters.