On 10/19/06, Kevin Tew <tewk / tewk.com> wrote:
> Java does this also.  The String class is immutable.

And the resulting ugliness of string handling in Java is one of the
things I hate most about the language.

Until recently, the method for searching for text only worked for
immutable strings, and the method for replacing the text you found
only worked for mutable ones. Oh, the pleasure of doing a simple
search and replace...

> Java does this so that the VM can hash the string and take shortcuts to
> improve string performance.
[...]
> String concatenation becomes an expensive operation (copy required) that
> can not always be optimized away.

Yes, it becomes cheaper to turn a String into a StringBuffer, use
append, and convert it back, than to do a simple String + operation.
Which is surprising to many, so much so that there are lots of
articles for Java developers trying to explain it. e.g.
<URL:http://www.javaworld.com/javaworld/jw-03-2000/jw-0324-javaperf.html>

So while I understand the motivation for making all strings immutable,
it's one of those cases where I suspect the cure is worse than the
disease. (Java's exception handling is another.)


mathew
-- 
<URL:http://www.pobox.com/~meta/>