Return to Tutorial Index

Back to the home page of this site

morterboard

Java2 Certification
Tutorial

 

You can discuss this topic with others at http://www.jchq.net/discus
Read reviews and buy a Java Certification book at http://www.jchq.net/bookreviews/jcertbooks.htm


9) The java.lang package

Objective 2)

Note on this objective

Descibe 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"); 
        System.out.println(s1); 
        s1=s2;
        System.out.println(s1);
        }
}

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 theorectical 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.

 

 

Questions

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?

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

3)
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


Answers

Answer 1)

4) None of the above
Once created a String is read only and cannot be changed Each one of the options actually creates a new string "behind the scenes" and does not change the original. If that seems to go against your experience and understanding read through information on the immuatbility of strings


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 topc

This topic is covered in the Sun Tutorial at
http://java.sun.com/docs/books/tutorial/essential/strings/stringsAndJavac.html
(doesn't go into much detail)

Jyothi Krishnan on this topic at
http://www.geocities.com/SiliconValley/Network/3693/obj_sec9.html#obj29

Last updated
16 Sep 2000
copyright © Marcus Green 2000
most recent version at www.jchq.net