What is the difference between String and StringBuffer? Which is better to use in project?

Showing Answers 1 - 28 of 28 Answers

venkatram reddy

  • Oct 25th, 2005
 

String buffer is a mutable string object. String is a non - mutable object. When we say mutable means which can be changed anytime. Its better to string buffer if we want to modify a string from multiple threads(Some places synchronization is required).

 

 

  Was this answer useful?  Yes

venkatram reddy

  • Oct 25th, 2005
 

String buffer is a mutable string object. String is a non - mutable object. When we say mutable means which can be changed anytime. Its better to string buffer if we want to modify a string from multiple threads(Some places synchronization is required).

 

 

  Was this answer useful?  Yes

SRIRAM.N.SHARMA

  • Oct 30th, 2005
 

When a String is created, it is not destroyed. It remains in the pool of memory and whenever that string is referred, the JVM searches for the String from the string pool of memory and gets the relevant data. So as such String is expensive operation.

String buffer can be changed/modified based on our requirement. There are chances that you may need to form a query based on some conditions. Such queries can be formed using String buffer. Finally you can user the toString method to get the final String version of the stringBuffer.

Pls mail back if you are unclear with any of the items that I have specified in this solution.

Thanks & Regards,

Sriram

  Was this answer useful?  Yes

Manikant

  • Oct 31st, 2005
 

String is immutable whereas StringBuffer is mutable.

  Was this answer useful?  Yes

Manikant

  • Oct 31st, 2005
 

String is immutable whereas StringBuffer is mutable.

  Was this answer useful?  Yes

justAnother

  • Nov 3rd, 2005
 

I just read a discussion in the following article (about String vs Stringbuffer). It contridicts popular belief and so it is interesting.

http://thedailywtf.com/forums/41017/ShowPost.aspx

Hope that is informative/ interesting.

- justAnother

  Was this answer useful?  Yes

jagadeesh meduri

  • Nov 6th, 2005
 

RE: What is the difference between String and StringBu...

  Was this answer useful?  Yes

sekhar babu

  • Nov 10th, 2005
 

String class objects are immutable(not change).StringBuffer class objects are mutable(change).In our project for constants use String class object for variables use StringBuffer class object ,then performance will be increased.

  Was this answer useful?  Yes

mandyjoshi

  • Nov 11th, 2005
 

String is immutable. If you want to update string value often (build query on condition) you should use StringBuffer.

for(int i=0;i<100;i++)

{

}

  Was this answer useful?  Yes

mandyjoshi

  • Nov 11th, 2005
 

String is immutable. If you want to update string value often (build query on condition) you should use StringBuffer.

method(){

String abc = "";

for(int i=0;i<100;i++)

{

    abc = abc + "d"; // each time new string object is formed. very expensive

}}

  Was this answer useful?  Yes

Deepti Kajjam

  • May 2nd, 2007
 

String class is used to manipulate character strings that cannot be changed. The objects of type String are read only and immutable.

The StringBuffer class is used to represent characters that can be modified during the program.

The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations.


Text can be appended only to an end of the String whereas it can be appended any where to a StringBuffer.

Examples:

String str="Good";
str+="Morning";      // Appended to its end.

StringBuffer str=new StringBuffer("God");
str.append("Morning");
str.setcharAt(2,'o');      //Inserts 'o' in between the string "God"

  Was this answer useful?  Yes

Shipra Jain

  • May 11th, 2007
 

String is mutable and String buffer is immutable.

  Was this answer useful?  Yes

rahul

  • May 26th, 2007
 

hi
will u tell me about this difference in detail/
String s1= "hello";
String s2= new String("hello");

  Was this answer useful?  Yes

rakesh

  • Aug 18th, 2007
 

String is non-mutable suppose u have declared
String testString="init text";
          testString="new text"

in this case testString is created again in memory pool with new text.

But in case of StringBuffer these are mutable types you can at any time change their values or append new stuff in their values.

  Was this answer useful?  Yes

String objects are constants and immutable where as StringBuffer objects are not.StringBuffer Class supports growable and modifiable string where as String class supports constant strings. Strings once created we cant modify them. Any such attempt will lead to the creation of new strings.  Where as StingBuffer objects after creation also can be able to delete or append any
characters to it. String values are resolved at run time where as StringBuffer values are resolved at compile time. So if you know the exact size of the string to be appended it is better to go for String objects.

  Was this answer useful?  Yes

String is a immutable.What is immutable the state of an object is not changed if we initilize once.i.e for example String s1="Geek"
                                                    s1="Interview"
If we try to print s1 it print's "Interview" then where is the immutability.See when we write s1="Geek" the jvm prepares "Geek" string object and it is assigned to s1.and
next we did s1="Interview" what happens here it creates another string object "Interview" and it is assigned to s1 which is already present string reference.then what about the "Geek" object.It is garbege collected.

But StringBuffer is mutable i.e the state of an object is changed.

  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