What is the Difference Between String and StringBuffer? What is the Conclusion of String str="abc"; and String str= "abc"+"c"?
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   pravin_kumar
Posted On:   Saturday, June 26, 2004 02:50 AM

			
What is the Difference Between String and StringBuffer? What is the Conclusion of String str="abc"; and
String str= "abc"+"c"?

Re: What is the Difference Between String and StringBuffer? What is the Conclusion of String str="abc"; and String str= "abc"+"c"?

Posted By:   Daniel_Eriksson  
Posted On:   Wednesday, September 8, 2004 04:26 AM

Creating a new String uses the "string constant pool", that if, the string does not exist, creates it. All strings created that 'equals' each other points to one 'object' in the "string constant pool". Creating new strings therefore uses a lot of memory, especially using temporary values inside loops.

StringBuffer on the other hand grows in memory as new strings are appended to it.

Re: What is the Difference Between String and StringBuffer? What is the Conclusion of String str="abc"; and String str= "abc"+"c"?

Posted By:   Balamurugan_Samy  
Posted On:   Friday, August 27, 2004 10:58 AM

What is the Difference Between String and StringBuffer? What is the Conclusion of String str="abc"; and String str= "abc"+"c"?

Re: What is the Difference Between String and StringBuffer? What is the Conclusion of String str="abc"; and String str= "abc"+"c"?

Posted By:   Balamurugan_Samy  
Posted On:   Friday, August 27, 2004 10:45 AM

What is the Difference Between String and StringBuffer? What is the Conclusion of String str="abc"; and
String str= "abc"+"c"?

Re: What is the Difference Between String and StringBuffer? What is the Conclusion of String str="abc"; and String str= "abc"+"c"?

Posted By:   Simon_Ablett  
Posted On:   Monday, June 28, 2004 06:32 AM

A String object is immutable. That is to say that once set its contents cannot be changed. So if you set a string 's1' equal to 'abc' and then add 'c' to it what you are doing is creating two temporary strings for 'abc' and 'c' and then a third for 'abcc' (not very efficient). The contents of a StringBuffer on the other hand can be changed and the memory allocated to store it can grow as needed.

Regards.
About | Sitemap | Contact