GeekInterview.com
Series: Subject: Topic:
Question: 930 of 2109

what is the difference between string and string buffer?

Asked by: Interview Candidate | Asked on: Nov 15th, 2005
Showing Answers 1 - 25 of 25 Answers
DevikaBhatt

Answered On : Nov 15th, 2005

String objects are immutable. StringBuffer supports mutable string.

Yes  1 User has rated as useful.
  
Login to rate this answer.
nirmala

Answered On : Nov 15th, 2005

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

  
Login to rate this answer.
nirmala

Answered On : Nov 15th, 2005

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

  
Login to rate this answer.
sunil

Answered On : Nov 16th, 2005

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

  
Login to rate this answer.

String : String objects are constant & immutableString Buffer : String Buffer objects are not constant & growable

  
Login to rate this answer.
sumanthdivvela

Answered On : 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

  
Login to rate this answer.
shiv shankar

Answered On : 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 .

  
Login to rate this answer.
Mokani D. J.

Answered On : 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.

  
Login to rate this answer.
sureshlg

Answered On : Dec 1st, 2005

View all answers by sureshlg

HIthis is sureshString s="India";String s2=s;s.repalce(n.k)//this statement compile error bzc string immatables=s+"great";//in this case will created new reference but value not changed

  
Login to rate this answer.
Murali

Answered On : Dec 20th, 2005

The String datatype is immutable where as StringBuffer is muttable

  
Login to rate this answer.
santosh..

Answered On : 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 changeablewhile 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.  

  
Login to rate this answer.
Srinivasa Reddy Tanikonda

Answered On : Jan 3rd, 2006

1. Strings are immutable where as stringbuffer are not2. 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 resolutionRun 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 advance3. Some intersting things about String BufferStringBuffer 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.

  
Login to rate this answer.
ramalinga reddy

Answered On : Jan 6th, 2006

Pl explain practically what is difference between String and StringBuffer

  
Login to rate this answer.
suresh Kumar Saini

Answered On : 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

  
Login to rate this answer.
vai_ir

Answered On : Mar 21st, 2006

View all answers by vai_ir

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

  
Login to rate this answer.
srinivas.bora

Answered On : 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 idbora_srinivasarao@yahoo.co.in     keeps smiling and mailing    bora_srinivasarao@yahoo.co.in

  
Login to rate this answer.
wamiq

Answered On : 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'.

  
Login to rate this answer.
Sai Sanjay K

Answered On : Apr 6th, 2006

This is the best answer I have seen.

  
Login to rate this answer.
srinivas

Answered On : May 17th, 2006

hi,all are tell the difference which is string objects are immutable andStringBuffer objects are muttable.But any one don't give reason.the main reason is String Objects immutable because the time takento create new object is less than to re allocate memory for existingobject.in java more of the situations we use String objects.ok baye...................keeps smiling and mailingbora_srinivasarao@yahoo.co.inif any want java material & doubts regarding j2se ,j2ee plz send mail tobora_srinivasarao@yahoo.co.in

  
Login to rate this answer.
padma

Answered On : 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.

  
Login to rate this answer.
Sinan

Answered On : 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

  
Login to rate this answer.
rubyy

Answered On : Dec 7th, 2007

View all answers by rubyy

I dont know i want to ask

  
Login to rate this answer.

String is mutable which means we cont possible to change that velue and StringBuffer is immutablue so we can add delete and append to that string

  
Login to rate this answer.
srinivaspulla

Answered On : Sep 7th, 2008

View all answers by srinivaspulla

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.

  
Login to rate this answer.
Tarun Wadhwa

Answered On : Sep 8th, 2008

View all answers by Tarun Wadhwa

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

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.