Originally posted by saikrishna cinux:
hi String class has got final class and private variables so it's state is not changed
In the sam way Stringbuffer has also got finla class and private variables but it is not called as immutable class ,why?
:roll:
can any one please justiy
Hai,
Actaully String class is using the FLYWEIGHT
pattern inside. With the usage of the pattern one can accomadate more objects with same data. Let me put in this way, for example assume 100 people are accessing a application, in that first person created a string hello, and if other 99 people are also trying to create the work hello, there will be only one
word hello in the pool and all the references will be pointed to it.
So it is not good to change this as every one is pointing to this one. Thats why strings are made immutable.
Hope I have given clear explanation.