what is difference between String buffer and String builder?

Answer Posted / mohan

String is immutable whereas StringBuffer and StringBuilder
can change their values.

The only difference between StringBuffer and StringBuilder
is that StringBuilder is unsynchronized whereas StringBuffer
is synchronized. So when the application needs to be run
only in a single thread then it is better to use
StringBuilder. StringBuilder is more efficient than
StringBuffer.

Criteria to choose among String, StringBuffer and StringBuilder

---If your text is not going to change use a string Class
because a String object is immutable.
---If your text can change and will only be accessed from a
single thread, use a StringBuilder because StringBuilder is
unsynchronized.
---If your text can changes, and will be accessed from
multiple threads, use a StringBuffer because StringBuffer is
synchronous.

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is java algorithm?

519


How can we make sure main() is the last thread to finish in java program?

667


What is a generic data type?

569


Why synchronization is important in java?

558


What is a parameter in java?

542






What is a stack class in java ?

607


What is the association?

543


explain copyonwritearraylist and when do we use copyonwritearraylist?

537


How can you make sure that your singleton class will always return single instance in multi-threaded environment?

566


How are variables stored?

527


How do you calculate roots in java?

522


List two java ide’s?

606


Which method returns the length of a string?

562


Is main a function?

510


How to run a JAR file through command prompt?

712