Thursday, October 2, 2008

Why Strings are immutable?

From all the objects in the java world the String object is the most exceptional one. It's the only object that you can use the operators + and += on it. This is the only object that you can define like String myString = "moshe". And the most important feature that affects all of us is that Strings are immutable? Why? What so special in Strings and why it's not like it in different languages? In fact strings are very important and unique objects in java for the following reasons:
Java is the internet and networking language. Therefore, we will need a language that supports reading text very well. Therefore, we don't want to allocate new string for every string object. The solution: java creates a constant pool for strings each string that is allocated in compile time is allocated in the pool so we will reduce creation of redundant object. Therefore, the string must be immutable, since if not changing in one reference of the string will change the string in other references of the program.
Since string is so basic in java, the java developers would like to give the string the essence of primitive. Since string is immutable it looks like it pass by value for a method (if the string has changed is no longer the same object) so String is immutable like all the other wrappers of the primitives.
Since Strings are very popular mainly for configuration, it is highly important that the String will be thread safe without paying the cost of synchronization therefore, using immutable object is highly recommended here.
String is one of the basic objects in java this is why it's so unique. It is highly important to understand its place in the java world.


babjee said...

you are mentioned that uses of Strings because of they are immutable.can u tell me why sun people made Strings are IMMUTABLE?

Anonymous said...

actually, i think that String is like a mutation of an object and a primitive. all primitives don't need any garbage collections for assigning new values.String is used so much, that it was forced to work like a primitive, but in order to make you know that it's still an object underneath it all, it has a capital letter just like any other class.
why they made it immutable ? well i wouldn't have done it if i was the java creator. instead, i would give it a different name (maybe "ReadOnlyString") , and offer other similar classes too, just like those that are already on Java.
however, it's still only naming. if you want a mutable String, use either StringBuffer or StringBuilder , which are both faster than normal String, at least for concatanation (JIT might do some optimizations on the normal String which cannot be possible on the other classes) .
StringBuffer is synchronized and StringBuilder is not (but is faster).

Anonymous said...

a small reason for having Strings mutable: lower memory usage. if many string instances have the same value , they can all simply point to the same object.
maybe the code behind the string also has something to do with it being mutable-it might work like an array, which as you know , has a constant length.
another claim about the reason for String being immutable is security for multi-threading, so that the string won't change its value while other threads read it. however, this is quite a lame claim, since all of the primitives can change while other threads read them.
here's some more info: