What is the use of immutable String class in java?

Showing Answers 1 - 9 of 9 Answers

kranti

  • Jan 30th, 2006
 

String Objects are immutable where as String references are not.

 ex : String s="abc";// here two objects are created and one reference s

                           // where one is copied in the pool and another in non pool

       s.concat("def");  // here s refernces still to abc since it is not refrenced to any value.

     s1=s.replace('a','x'); // here s1 value is xbc ... s still refers to abc ..

kranti k

  Was this answer useful?  Yes

TDeepika

  • Mar 7th, 2006
 

String Objects are immutable .They cant be changed. StringBuffers are mutable.they can be altered as per the users wish.

  Was this answer useful?  Yes

srinuhere

  • Mar 17th, 2006
 

If your value is not going to change and if u don't want to multiple threads to access...then strings come into picture

  Was this answer useful?  Yes

uafshahid

  • Apr 30th, 2006
 

Immutable means not changeable. when a string is declared and initiated with some values.

Afterwards whenever we will concat that string or change that string. if we r going to add more characters in that string object. it will not allow dynamic size change. to cater such problem when any change will occur in string object the object will be recreated and previous object will be destroyed and new will be created with new size to accomodate change. thats y strings are immutable in java.

  Was this answer useful?  Yes

rsaneve

  • Jul 19th, 2006
 

String are made immutable because

1. better memory management.Generally thousands of string literals objects are created in any of the application.To avoid the individual memory allocation to all the string objects ,JVM creates the string literal objects in a dedicated string constants memory pool and allots the a same reference to all the string having the same value.

String s1 = "abc";

String s1 = "abc"; //here both s1 , s2 point to the same object abc,rather than 2 different objects with same value.

And so the String class is made final to support this functionality.

  Was this answer useful?  Yes

rsaneve

  • Jul 19th, 2006
 

String are made immutable because

1. better memory management.Generally thousands of string literals objects are created in any of the application.To avoid the individual memory allocation to all the string objects ,JVM creates the string literal objects in a dedicated string constants memory pool and allots the a same reference to all the string having the same value.

String s1 = "abc";

String s2 = "abc"; //here both s1 , s2 point to the same object abc,rather than 2 different objects with same value.

And so the String class is made final to support this functionality.

  Was this answer useful?  Yes

javabuddy

  • Jan 26th, 2011
 

String is a special class and it has handled specially in JAVA.

There are several benefits of String being immutable and there are certain things which are possible only because String is immutable.

1) Since String is immutable it can safely share between many threads, which are very
important for multithreaded programming.

2) Because of immutability we can store String in String Pool, had it been mutable it’s not possible to implement such idea.

3) String immutability allows to cache its hash value which greatly improves String performance in hash based collections.

 

Thanks
Javin

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions