Monica. Shiralkar wrote:Why so?
Have you had a look at any of the posts that Fred suggested?
However, just a few advantages off the top of my head:
1. It makes Strings Thread-safe.2. It means that they are safe for use as keys in Maps.3. It means that their hashcode can be cached as and when needed.4. It means that their internals can be shared between instances. substring(), for example, does NOT copy the internal character array, it simply creates a new String with a different start offset and length.
Also, there is
already a mutable "String" class (in fact, two of them) -
StringBuilder. So why would you
not want String to be immutable?
My advice: If you're writing a class of your own, make it immutable
unless you have a very good reason not to. It just makes life easier.
Winston