what is diff string and stringbuffer

Answer Posted / monika

string is immutable, that is it can not be extended.whereas
StringBuffer is mutable and can be extended.
Fro example :
Consider 2 statement : "Welcome " and "to Java World".
now assign first statement to string and string buffer.
String str = "Welcome" & StringBuffer strBuff = new
StringBuffer("Welcome");
Now if we add 2nd statement to both then :
str= str + "to Java World" -> In this case, it would dump
all the memory allocated with "welcome" and allocate a new
memory space to the entire string "Welcome to Java World" .
On the other hand, in strBuff :-
strBuff.append("to Java World") -> if simply allocate a
new memory space to only 2nd statement and add to link to
previous name.

Is This Answer Correct ?    51 Yes 7 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Which sorting algorithm is used by collections.sort() in java ?

731


Hi we have an urgent requirement for Java/J2ee technical lead position & also looking for "Java Guidewire claimcentre" experienced professional for Bangalore location if interested can reach srisanh@gmail.com

2742


What is java collection? : java collections

809


What are the classes in the java collection framework? : java collections

803


What is the difference between comparable and comparator in java.util pkg?

808


What are the different types of classes implemented in the set interfaces? : java collections

823


What is the use of hashcode in java ?

787


What are the uses of the set interfaces in the java collections? : java collections

745


my interviewer asked me what technical specification you used how to answer that question

2104


What are the different types of collections views being provided by the map interface? : java collections

789


What is the use of the list interface in the java collection? : java collections

784


What are the types of the main implementing classes in the map interfaces? : java collections

834


please mail me the interview question based on java/j2ee

1718


What is the enumerator of the java collection framework? : java collections

759


What are the types of java collections? : java collections

728