View Diksha  Basu 's Profile
What is difference between String, StringBuffer and StringBuilder? When to use them?
Below is the main difference between these three most commonly used classes.

String class objects are immutable whereas StringBuffer and StringBuilder objects are mutable.
StringBuffer is synchronized while StringBuilder is not synchronized.
Concatenation operator "+" is internal implemented using either StringBuffer or StringBuilder.

Criteria to choose among String, StringBuffer and StringBuilder

If the Object value is not going to change use String Class because a String object is immutable.
If the Object value can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.
In case the Object value can change, and will be modified by multiple threads, use a StringBuffer because StringBuffer is synchronized.
Asked by Diksha Basu | Jun 19 2017 | Report this Report abuse
Replies (5)
View Roshan  shah 's Profile
Well written and described in good words . I am having one question

How to create a immutable object in Java? Does all property of immutable object needs to be final?
Jun 22 2017 | Report this Report abuse
View Shatish  Rao 's Profile
To create a object immutable You need to make the class final and all its member final so that once objects gets crated no one can modify its state. You can achieve same functionality by making member as non final but private and not modifying them except in constructor. Also its NOT necessary to have all the properties final since you can achieve same functionality by making member as non final but private and not modifying them except in constructor.


hope i am clear
Jun 22 2017 | Report this Report abuse
View Nirupama  Pathak 's Profile
Immutable Class:

class should be final
no setter methods
should be change Object state throught Constructor only
Jun 21 2017 | Report this Report abuse
View Tanya  Sharma 's Profile
Immutable Class:

class should be final
no setter methods
should be change Object state throught Constructor only
Jun 21 2017 | Report this Report abuse
View Shatish  Rao 's Profile
String is an immutable class while StringBuilder & StringBuffer are mutable classes. StringBuffer is synchronized while StringBuilder is not.

Below link can be useful to find more differences between String, StringBuffer & StringBuilder

Difference between String, StringBuffer and StringBuilder
Jun 20 2017 | Report this Report abuse