Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
May 13, 1997

Java Strings: Mutable or immutable?

Question:
I don't understand why it is said that Strings are immutable in Java. Here is a small piece of code which shows that Strings are in fact mutable:
class TestStringStuff{

public static void main (String args[]){
	String name = "Bill";
	System.out.println(name);
	name = name +" Clinton";
	System.out.println(name); // name has been                                   // changed from                                    // Bill to Bill                                    // Clinton
}
}  

Answer:
First a little philosophy:

An object is mutable (i.e. variable) if some of its properties can be changed without changing its essential identity. People are mutable objects. A person can get a face lift, a sex change, dye his hair, diet, and change his name, but we still regard him as the same person. (Philosophers are still arguing about what might be considered the "essence" of a person.)

Arrays are mutable. We can change an individual component of an array x:

x[i] = x[i] + 1;
but we still regard x as the same array.

By contrast, changing any property of a non-mutable object (i.e. a constant) changes the object's identity. For example, a rational number like 3/4 is non-mutable. It has two properties: a denominator and a numerator. If we change the numerator from 3 to 1, we regard the result, 1/4, as a completely different number.

Java strings are non-mutable simply because the String class fails to provide mutators (methods that modify individual characters). The intention is to force programmers to treat strings as constants rather than variables. Similarly, instances of the Number, Character and Boolean classes are non-mutable.

Your example raises two issues. First, an object can merge with another object to form a larger object. For example, we can merge two strings using append:

 String first = "Bill",
          last = " Clinton",
          both = first + last;
but no one would say that the string "Bill" mutated into the string "Bill Clinton" any more than one would say the number 3 mutated into 7 after 4 was added to it.

Second, unlike people, we don't regard the name of an object as one of its properties. This is because we can always create anonymous instances of class:

new String("Bill");  // no name!
One might argue that the object to which a name refers is a property of the name, not the object. In this case, a name is mutable since we can always change its reference without changing its essential identity. Thus,
first = first + last;
changes the reference of first from "Bill" to "Bill Clinton," but we don't regard the left-hand occurrance of first as a symbol distinct from the right-hand occurance.

How do we modify individual characters within a string? Java provides a mutable counterpart to the String class called StringBuffer, which provides a mutator called insert. For example:

StringBuffer sbfirst = new StringBuffer(first);
   sbfirst.insert(1, 'u');  // change 'i' to 'u'
   first = mfirst.toString();
In fact, the expression:
 first + last
is compiled to:
new StringBuffer().append(first).append(last).toString()
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date