What is the difference between string and string buffer?

Showing Answers 1 - 25 of 25 Answers

DevikaBhatt

  • Nov 15th, 2005
 

String objects are immutable. StringBuffer supports mutable string.

nirmala

  • Nov 15th, 2005
 

string objects are constants & immutablestring Buffer objects are not constants & growable

  Was this answer useful?  Yes

nirmala

  • Nov 15th, 2005
 

string objects are constants & immutablestring buffer objects are not constants & growable

  Was this answer useful?  Yes

sunil

  • Nov 16th, 2005
 

String is immutable & unchagable.In this situation String buffer provide the fact.we can modify our required string by it.

  Was this answer useful?  Yes

sumanthdivvela

  • Nov 22nd, 2005
 

Dear Sir/Madam, You told that there is difference between string and stringbuffer, i understand but i need pactical implementation so could you please give some example for the difference because i am new to java technology i know only basics. Thank you

  Was this answer useful?  Yes

shiv shankar

  • Nov 22nd, 2005
 

In String class the equals() from Object is overridden i.e whenever u compare the two strings it will compare the conent in that strings. In String Buffer class the equals method is from object class not overridden .

  Was this answer useful?  Yes

Mokani D. J.

  • Nov 23rd, 2005
 

String variable is immutable. So once you declare it you are not able to modify it. But StringBuffer class is mutable. You can change the content of StringBuffer variable.

  Was this answer useful?  Yes

sureshlg

  • Dec 1st, 2005
 

HI

this is suresh

String s="India";

String s2=s;

s.repalce(n.k)//this statement compile error bzc string immatable

s=s+"great";//in this case will created new reference but value not changed

  Was this answer useful?  Yes

Murali

  • Dec 20th, 2005
 

the String datatype is immutable where as StringBuffer is muttable

  Was this answer useful?  Yes

santosh..

  • Dec 28th, 2005
 

Before i Say the difference between them u have to know the difference between mutable object & immutable object.

Mutable object are those object whose value can be changeable

while if you are not allowed to change the value of object then it is called called immutable.

Now stringbuffer is is mutable object while string is immutable object.

 

  Was this answer useful?  Yes

Srinivasa Reddy Tanikonda

  • Jan 3rd, 2006
 

1. Strings are immutable where as stringbuffer are not

2. StringBuffer.append() gives better perfomance than string if the concationation at runtime (Runtime resuliton).

+ operator between strings gives much better performance than StringBuffer.append() when it is compile time resolution

Run time resolution takes place when the value of the string is not known in advance where as compile time resolution happens when the value of the string is known in advance

3. Some intersting things about String Buffer

StringBuffer maintains a character array internally.The default capacity is 16 characters. When the StringBuffer reaches its maximum capacity, it will increase its size by twice the size plus 2 ( 2*old size +2).

 If you use default size, initially and go on adding characters, then it increases its size by 34(2*16 +2) after it adds 16th character and it increases its size by 70(2*34+2) after it adds 34th character. Whenever it reaches its maximum capacity it has to create a new character array and recopy old and new characters. It is obviously expensive. So it is always good to initialize with proper size that gives very good performance.

  Was this answer useful?  Yes

ramalinga reddy

  • Jan 6th, 2006
 

pl explain practically what is difference between String and StringBuffer

  Was this answer useful?  Yes

suresh Kumar Saini

  • Jan 19th, 2006
 

String Class object allocate fix memory if we assign different object to the same reference then previousely reference will be lost or we can not access previous value

but stringBuffer keeps some extra memory

  Was this answer useful?  Yes

vai_ir

  • Mar 21st, 2006
 

String is Immutable objects,while Stringbuffer are muttable objects.secondlly String class is final ,Stringbuffer is not final.

  Was this answer useful?  Yes

srinivas.bora

  • Mar 21st, 2006
 

hi friends,

string objects are immutable.

      string buffer objects mutable.

    - the methods which modifies the content of the objects are defined as string buffer class.

      the methods which do not modifies the content of the objects are defined in string class.

okey baye.....
If there is any questions on j2se and j2ee plz send to my mail id
bora_srinivasarao@yahoo.co.in

     keeps smiling and mailing
    bora_srinivasarao@yahoo.co.in

  Was this answer useful?  Yes

wamiq

  • Mar 23rd, 2006
 

Its right String is immutable(that can not be changed) character sequence whereas StringBuffer is not and it is growable.e.g.:-String name= new String("what");// name.setCharAt(2,'a');//gives error.Stringbuffer name = new stringbuffer("what");name.setcharAt(2,'a');//does not give error and modify the string as 'waat'.

  Was this answer useful?  Yes

Sai Sanjay K

  • Apr 6th, 2006
 

This is the best answer I have seen.

  Was this answer useful?  Yes

srinivas

  • May 17th, 2006
 

hi,

all are tell the difference which is string objects are immutable and

StringBuffer objects are muttable.But any one don't give reason.

the main reason is String Objects immutable because the time taken

to create new object is less than to re allocate memory for existing

object.in java more of the situations we use String objects.

ok baye...................

keeps smiling and mailing

bora_srinivasarao@yahoo.co.in

if any want java material & doubts regarding j2se ,j2ee plz send mail to

bora_srinivasarao@yahoo.co.in

  Was this answer useful?  Yes

padma

  • Dec 18th, 2006
 

Java provides the StringBuffer and String classes, and the String class is used to manipulate character strings that cannot be changed. Simply stated, objects of type String are read only and immutable. The StringBuffer class is used to represent characters that can be modified.

The significant performance difference between these two classes is that StringBuffer is faster than String when performing simple concatenations. In String manipulation code, character strings are routinely concatenated. Using the String class, concatenations are typically performed as follows:

                       

     String str = new String ("Stanford  ");

     str += "Lost!!";     

If you were to use StringBuffer to perform the same concatenation, you would need code that looks like this:

                       

     StringBuffer str = new StringBuffer ("Stanford ");

     str.append("Lost!!");   

Developers usually assume that the first example above is more efficient because they think that the second example, which uses the append method for concatenation, is more costly than the first example, which uses the + operator to concatenate two String objects.

The + operator appears innocent, but the code generated produces some surprises. Using a StringBuffer for concatenation can in fact produce code that is significantly faster than using a String. To discover why this is the case, we must examine the generated bytecode.

  Was this answer useful?  Yes

Sinan

  • Nov 3rd, 2007
 

Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared

  Was this answer useful?  Yes

Java provides the StringBuffer and String classes, and the String class is used to manipulate character strings that cannot be changed. Simply stated, objects of type String are read only and immutable. The StringBuffer class is used to represent characters that can be modified.


  Was this answer useful?  Yes

Strings are immutable objects means you cannot modify the instance. For example when you create one instance of string and you are changing the value of string, it does not mean that you are changing the value of string instance, it means that you are creating  a new instance for string.
While StrinBuffer is a muttable object means once you have created an instance of StringBuffer you can append the text without creating  a new Instance.

Example for String:
/* no full code*/
String str="Hello";
for(int i =0;i<=10;i++){
  str=str+" "+i;
//Here you are creating different different instance of String

}
Example for StringBuffere:
/* no full code*/
StringBuffer str=new StringBuffer("Hello");
for(int i =0;i<=10;i++){
  str.append(" "+i);
//Here u are appending  String not creating the instance

}
You wil get The Same output from Both The Programs
through the above examples u will find how we use String and Stringbuffer class

  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.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions