We have seen how variables of the primitive data type char
allow us to store and use individual letters and other characters, but more often than not the programs we will want to write will require storing more than just one character. For example, we might need to store someone's name, or a website address, or song lyrics, etc.
A String
is a special non-primitive type that stores data in the form of text (i.e., a "string" of characters).
Strings can be either constructed in a manner consistent with working with other objects, as shown below:
String myString = new String("Hello World");
...or strings may be declared and initialized using the shortcut:
String myString = "Hello World";
This is not typical of most other object types.
There are many useful features of the String class that we could discuss. We will discuss most of these in detail at a later point in time -- but for now, let us confine our attention to just three things:
Just like we can add numerical values together to get a new numerical value -- we can add strings together to get a new string. In java, adding two strings results in their concatenation, as seen in the code below. (Be aware, this is again not typical of other classes.†)
String s1 = "I like"; String s2 = "chocolate"; String s3 = s1 + s2; System.out.println(s3); //prints "I likechocolate" to the console
Notice the lack of a space between "like" and "chocolate". If we want a space between these two words, you'll need to explicitly put one into the string. This can be done in several ways. As one example, we could have made the third line above String s3 = s1 + " " + s2;
One can produce a boolean value (i.e., true
or false
) that will tell whether two strings contain the same text by invoking the equals()
method
CAUTION: The equality operator, "==
", can be used to test if certain types of values (i.e., byte, short, int, long, char, boolean) have the same value, but this will not always work correctly if it is used to compare two strings.
Think of the "value" of a string variable as a reference to where the string is stored in memory. If two string variables, s1
and s2
reference the same portion of memory, they certainly represent the same string and "s1 ==s 2
" will return true
. However, it is also possible to have s1
and s2
reference two identical strings in different portions of memory, in which case "s1 ==s2
" will return false
.
Unfortunately, determining which of these two situations apply is not always obvious. Consider the following:
String s1 = "bob"; String s2 = "" + "bob"; String s3 = ""; s3 += "bob"; System.out.println(s1 == s2); // prints "true" System.out.println(s1 == s3); // prints "false" System.out.println(s1.equals(s2)); // prints "true" System.out.println(s1.equals(s3)); // prints "true"
One can grab the $n^{\textrm{th}}$ character that appears in a string with the charAt()
method. Note, the first character of the string is associated with $n=0$, the second with $n=1$, the third with $n=2$, and so on... The length of a string s
can be found with s.length()
. As an example, consider the following:
String s = "Once upon a midnight dreary..."; char c = s.charAt(5); //c is now the character 'u'