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 | Sep 8 2018 | Report this Report abuse
Replies (6)
View Tanya  Sharma '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
Sep 17 2018 | Report this Report abuse
View Roshan  shah '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
Sep 16 2018 | Report this Report abuse
View Nirupama  Pathak '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?
Sep 15 2018 | Report this Report abuse
View Shatish  Rao 's Profile
Immutable Class:

class should be final
no setter methods
should be change Object state throught Constructor only
Sep 15 2018 | Report this Report abuse
View Shatish  Rao 's Profile
Immutable Class:

class should be final
no setter methods
should be change Object state throught Constructor only
Sep 15 2018 | 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
Sep 15 2018 | Report this Report abuse