Why String is immutable?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Why String is immutable?

Bharath Ganesh
Hi
 
Could anyone tell me why String is immutable?


=====
cheerio
bharath
I Blog at http://jroller.com/page/bharath

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

RE: Why String is immutable?

David Holmes
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.
 
Cheers,
David Holmes
-----Original Message-----
From: [hidden email] [mailto:[hidden email]]On Behalf Of Bharath Ganesh
Sent: Wednesday, 14 September 2005 7:40 PM
To: [hidden email]
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
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Why String is immutable?

Gregg Wonderly-2
In reply to this post by Bharath Ganesh


Bharath Ganesh wrote:
> Hi
>  
> Could anyone tell me why String is immutable?

There are several reasons, but the one that generally has the largest impact is, so that a single instance of any string
value can be used for all references.  Look at String.intern(), which is used to maintain a "weak hash map" of all
string values. This greatly reduces the total space for String values in many applications.  It would be interesting,
to have the ability to turn that off, and then see the difference in heap use for an XML based application for instance.

Gregg Wonderly
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Why String is immutable?

Osvaldo Pinali Doederlein
In reply to this post by David Holmes
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).


A+
Osvaldo

> Cheers,
> David Holmes
>
>     -----Original Message-----
>     *From:* [hidden email]
>     [mailto:[hidden email]]*On Behalf Of
>     *Bharath Ganesh
>     *Sent:* Wednesday, 14 September 2005 7:40 PM
>     *To:* [hidden email]
>     *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
> [hidden email]
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


--
-----------------------------------------------------------------------
Osvaldo Pinali Doederlein                   Visionnaire Inform├ítica S/A
[hidden email]                http://www.visionnaire.com.br
Arquiteto de Tecnologia                          +55 (41) 337-1000 #223

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest