Immutable means you cannot change the contents of an object. For String, this means that once a String object has been created, it remains the same for as long as the String object exists. If you need a mutable "string" look at StringBuffer in Java 1.4 and StringBuilder in Java 5 and above.
The reason why String is immutable is mostly memory management I think, but I don't know the exact reason.
Originally posted by Rob Prime: The reason why String is immutable is mostly memory management I think, but I don't know the exact reason.
I can't speak for the engineers that made the decision, but I've heard justification to say that it has more to do with security. Not to mention, Joshua Bloch, a very prominent (ex) Sun engineer who worked on the java class libraries wrote a book called effective java that stated that all classes should be immutable unless there is a good reason for them to be mutable.
Joined: Jul 17, 2006
I can't find anything in the white papers that says why specifically, but consider the following case:
The white papers do specifically say that they followed c++'s example by allowing a string literal, and implicitly creating a new object of type String to represent that literal. i.e. the "quoted string" concept is extended into java.
Now, the java language spec says that two identical string literals are actually the same object -- that is, they have the same identity, the == operation will return true. This is accomplished via the String.intern method. The jvm maintains a pool of Strings. The intern method will insert the string to that pool if it is missing, and if it isn't, it will return the copy of that string that is currently in the pool.
A consequence of this is, if strings were mutable, then changing it from one reference would effect all other references. Consider the following example, which uses pretend methods for modifying a String:
If strings were mutable, the variable hello no longer contained the word "hello". This is a serious problem. Hence, for this reason, Strings must be immutable.
Joined: Jul 17, 2006
The API for string suggests another good reason for strings to be immutable. Since they are immutable, they can be shared (this is basically exactly what I was talking about with the jvm string pool, but has a different consequence).
So, we say that as String is "thread-safe". That means that we can use it on more than one thread without worrying about what is happening to it on another thread.
For example, let us consider a c-style string, for a minute. In c, we know that we need to have \0 at the end of every string, so that we know where the end of the string is.
So lets say we have the string "Hello". On one thread, we decide to append " World" to it. So we start by writing the space (code 32) in index 5, and 'W' in index 6, etc.
Now lets say that after we've put the W in position 6, but before we put the o in position 7, a DIFFERENT thread decides to read the string. But now, the string has no \0 ANYWHERE. So it has no end.
We say that the string is in an inconsistent state, and this is very important in multithreaded programming.
Now, lets say that we cannot modify a string after its been created. Then it can NEVER be in an inconsistent state, so we know that it is thread safe.
This is a very good reason for a String to be immutable.
Yup - memory management may be part of it, but I think the biggest reason is the fact that in C++ a common error was for people to modify a string and forget that other parts of the program still had a reference to that same string, and expected to see it in its original form. I wouldn't say that strings [i]must[/i be immutable, but it's a good way to eliminate a large class of errors. Furthermore, multi-threaded applications have become more and more prominent (even if the users don't manage the threads themselves, e.g. in a servlet container) and sharing data between threads is much simpler if the data is immutable.