1.8 String Variables

You may have heard that Java is an object oriented language. Although there are only eight different primitive types, the number of object types is unlimited. Many are predefined but we can also create our own types of objects, to serve our own purposes. We will be studying objects very extensively later on but, for now, we will only be using one of the most common and useful types of objects available in Java - the string. Strings in Java are objects of type String (written exactly as shown, with an upper case S). We have already encountered string constants but we can also have string variables. To declare a variable of type String, we proceed as we would for any primitive type.

Example 1

The declaration

 

   String s;
 

will create a variable s of type String whose value is undefined, as shown in the next diagram.

To assign a value to a string variable, we can proceed in a manner similar to that used for assignments to primitive types. The result of such an assignment is, however, quite different from what we have seen with primitive types.

Example 2

If we have declared s to be of type String, we can assign a value to s by writing

 

 s = "Sample";

Here Java creates a new String object containing the string "Sample" but this is not stored in s. Instead, the value in s becomes a reference to the location in memory that contains the string. The result is illustrated in the next diagram where the arrow indicates that s contains a reference to the location occupied by the string.

Before going on, you should be sure that you understand what is happening here. A variable of type String, like s in our examples, does not actually contain a string. Instead, it contains a reference to the actual string object which is stored elsewhere in memory. Because s contains a reference to a string, we say that it is an example of a reference type variable (as opposed to a primitve type variable that actually contains its value).

Once we create a string, its value cannot be changed. Because of this, we say that strings are immutable objects. This does not mean that we cannot change values of variables that refer to strings.

Example 3

The fragment

 

   String s = "Joanne";
   s = "Jacqueline";

first sets the variable s to refer to a string object containing "Joanne" and then sets s to refer to a new string object containing "Jacqueline" (at which point the object containing "Joanne" is lost). The result is illustrated in the next diagram.

Although the string containing “Joanne” still exists, it is no longer accessible.

As well as assigning constants to string variables, we can also assign one string variable to another. The result is similar in some ways to that seen with assignments involving variables of primitive types: the value stored in one variable is copied into the other variable. Now, however, the copying involves references and so the effect is quite different, as shown in the next example.

Example 4

Suppose that we have created a string object by writing

 

   String s = "a string";
 

to produce the following situation:

If we now write

 

   String t = s;
 

the value in s will be copied into t. Since s and t now contain the same value and they are both reference variables, they both refer to the same string.

The print and println methods that we have been using to print string constants can also be used to print string variables. We can also use the concatenation operator to print any combination of string values.

Example 5

If the String variable answer has the value "Everest", then the statement

 

   System.out.println("The highest peak is " + answer + ".");
 

would print

 

Exercise 1.8

  1. Draw diagrams like those shown in the text to illustrate the result of executing the following statements.
    (a) 
    int a = 1;
       String b = "2";
    (b) String a = "first";
       String b = "second";
    (c) String a = "one";
       String b = a;
       a = "two";
    (d) String a = "ein";
       String b = "zwei";
       a = b;
       b = "drei";

 

  1. What would be printed by the following fragment?

 

   String c = "cat";
   String d =dog";
   String s = c;
   c = d;
   d = s;
   System.out.println("c is " + c);
   System.out.println("d is " + d);