Objective 2, The immutability of Strings

Describe the significance of the immutability of String objects

The theory of the immutability of the String class says that once created, a string can never be changed. Real life experience with Java programming implies that this is not true. 

Take the following code

public class ImString{
public static void main(String argv[]){
        String s1 = new String("Hello");
        String s2 = new String("There");

If Strings cannot be changed then s1 should still print out Hello, but if you try this snippet you will find that the second output is the string "There". What gives?

The immutability really refers to what the String reference points to. When s2 is assigned to s1 in the example, the String containing "Hello" in the String pool is no longer referenced and s1 now points to the same string as s2. The fact that the "Hello" string has not actually been modified is fairly theoretical as you can no longer "get at it".

The objective asks you to recognise the implications of the immutability of strings, and the main one seems to be that if you want to chop and change the contents of "strings" the StringBuffer class comes with more built in methods for the purpose.

Because concatenating string causes a new string to be instantiated "behind the scenes", there can be a performance overhead if you are manipulating large numbers of strings, such as reading in a large text file. Generally String immutability doesn't affect every day programming, but it will be questioned on the exam. Remember whatever round about way the question asks it, once created a String itself cannot be changed even if the reference to it is changed to point to some other String. This topic is linked to the way Strings are created in a "String pool", allowing identical strings to be re-used. This is covered in topic 5.2 as part of how the=operator and equals method acts when used with strings. Although neither the Java2 nor Java 1.1 objectives specifically mention it I am fairly confident that some questions require a knowledge of the StringBuffer class.


Question 1)

You have created two strings containing names. Thus

String fname="John";
String lname="String"

How can you go about changing these strings to take new values within the same block of code?

String fname=new String("Fred");
String lname=new String("Jones");

StringBuffer fname=new StringBuffer(fname);
StringBuffer lname=new StringBuffer(lname);

4) None of the above

Question 2)

You are creating a program to read in an 8MB text file. Each new line read adds to a String object but you are finding the performance sadly lacking. Which is the most likely explanation?

1) Java I/O is designed around a lowest common denominator and is inherently slow
2) The String class is unsuitable for I/O operations, a character array would be more suitable
3) Because strings are immutable a new String is created with each read, changing to a StringBuffer may increase performance
4) None of the above


Answer 1)

4) None of the above
Once created a String is read only and cannot be changed

Answer 2)

3) Because strings are immutable a new String is created with each read, changing to a StringBuffer may increase performance

I hope none of you C programmers suggested a character array?

Other sources on this topic

This topic is covered in the Sun Tutorial at

Jyothi Krishnan on this topic at