I Knew Java String is Immutable. Because we cannt change the value of String
object when it was created. explain with one code . thank you
A lot of String instantiations share their char[] data buffer without knowing who their
co-owners of the buffer are. Changing the buffer would result in changing all the
co-owners' buffers and result in a mess.
* The definition of "immutable" ?
* The design decision that String ought to be immutable?
* How String's API demonstrates string is immutable?
Hi,
I have some explanation to answer your query..
i) Definition of "immutable" : "immutable" means "unchange" or "not able to change".
ii)String ought to be immutable:
please try out the following example..it explains good...
[CODE=Java]class String_immutabl e{
public sttaic void main(String args[]){
String s1 = new String("StringV alue");
StringBuffer s2 = new StringBuffer("S tring buffer value");
s1.replace('a', 'b');
s2.replace(0,2, "zzz");
System.out.prin tln("\n String value after replace "+s1+" String buffer
after replace "+s2);
}
}[/CODE]
From above v clearly understand that "String" values are immutable and where as "String buffer" values are mutable.
iii) Api's:
you may try out with any API's . for better understanding "replace()" can be used..
Hi,
I have some explanation to answer your query..
i) Definition of "immutable" : "immutable" means "unchange" or "not able to change".
ii)String ought to be immutable:
please try out the following example..it explains good...
class String_immutabl e{
public sttaic void main(String args[]){
String s1 = new String("StringV alue");
StringBuffer s2 = new StringBuffer("S tring buffer value");
s1.replace('a', 'b');
s2.replace(0,2, "zzz");
System.out.prin tln("\n String value after replace "+s1+" String buffer
after replace "+s2);
}
}
From above v clearly understand that "String" values are immutable and where as "String buffer" values are mutable.
iii) Api's:
you may try out with any API's . for better understanding "replace()" can be used..
-Thanks & Regards,
Hamsa
Please use code tags, this thread might be useful for future members
Comment