what is the main difference between string and stringbuffer?
can you explain it with program?

Answer Posted / ashish ranjan

The main difference between String and StringBuffer is that String is Immutable, which means that we cannot modify the object which is created by the String.
As for Example.
String str = "abc";
now,
str = "abc" + "pqr";
the result is abcpqr. The previous value of str is not modified. It exists in the memory. Java Created new memory for str, which refers abcpqr.

now in case of StringBuffer
StringBuffer str = new StringBuffer("abc");
str.append("pqr");
it modifies in the same object.

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is instanceof keyword?

928


What are the loops in java?

729


Why main function is static?

869


What is an object’s lock and which object’s have locks?

680


What are inner classes or non static nested classes in java?

903


What are the different types of inheritance in java?

745


What is a stringbuilder?

685


What is set in java?

728


What is double parsedouble in java?

734


What are the steps in the jdbc connection?

753


What will happen if there is a default method conflict as mentioned above and we have specified the same signature method in the base class instead of overriding in the existing class ?

710


Why do we declare a class static?

732


What is the difference between synchronized and synchronized block?

757


Difference between abstract and concrete class ?

760


How do you make an arraylist empty in java?

661