[concurrency-interest] Why String is immutable?

Osvaldo Pinali Doederlein osvaldo at visionnaire.com.br
Wed Sep 14 10:37:33 EDT 2005

David Holmes wrote:
> Primarily for security purposes. It assures that given, for example, a 
> security check that you can access a file of a given name, that the 
> string is not concurrently modified to represent a different file 
> between the security check and the actual opening of the file.
> It also makes it easy to share strings without having to worry about 
> someone accidentally or maliciously changing the string - such as 
> constant values.

Besides these reasons plus memory savings (as pointed by Gregg), I
think that one major reason was avoiding synchronization.  Monitors
were much more expensive in the first releases of Java, strings are
used pervasively in any app, and Java was designed to make concurrent
programming easy (almost all APIs thread-safe); therefore, without
immutability, java.lang.String would have to be synchronized and this
would have a big performance impact (even with modern, high-perf JVMs).


> Cheers,
> David Holmes
>     -----Original Message-----
>     *From:* concurrency-interest-bounces at cs.oswego.edu
>     [mailto:concurrency-interest-bounces at cs.oswego.edu]*On Behalf Of
>     *Bharath Ganesh
>     *Sent:* Wednesday, 14 September 2005 7:40 PM
>     *To:* concurrency-interest at altair.cs.oswego.edu
>     *Subject:* [concurrency-interest] Why String is immutable?
>     Hi
>     Could anyone tell me why String is immutable?
>     =====
>     cheerio
>     bharath
>     I Blog at http://jroller.com/page/bharath 
> ------------------------------------------------------------------------
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

Osvaldo Pinali Doederlein                   Visionnaire Informática S/A
osvaldo at visionnaire.com.br                http://www.visionnaire.com.br
Arquiteto de Tecnologia                          +55 (41) 337-1000 #223

More information about the Concurrency-interest mailing list