Difference b/w String, StringBuffer, and StringBuilder

Author: neptune | 23rd-Sep-2022 | Views: 148

What is a String?

String is a sequence of characters in Java. String is a class in the java.lang package. String objects are immutable in nature. 

String str = "abc";

is equivalent to:

char data[] = {'a', 'b', 'c'};

String str = new String(data);

What is StringBuffer?

StringBuffer is a mutable sequence of characters in Java. StringBuffer is like a String but can be modified. String buffers are safe for use by multiple threads.

StringBuffer obj = new StringBuffer("Neptune");  



Output :

What is StringBuilder?

StringBuilder is a mutable sequence of characters. It is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type.

The append method always adds characters at the end of the builder; the insert method adds the characters at a specified point.

Instances of StringBuilder are not safe for use by multiple threads.

StringBuilder obj = new StringBuilder("Neptune");  



Output :

When do we use String?

If a string is going to remain constant throughout the program, then its recommended to use String because a String object is immutable in nature.

Example :

public static void ExampleString() {

String str = new String("Neptune");

String new_str = str + "World";



When do we use StringBuffer?

If a string can change and will be accessed by multiple threads, then use a StringBuffer because StringBuffer is synchronous, so you have thread-safety.

Example :
public static void ExampleStringBuffer() {

StringBuffer str_buff = new StringBuffer("Neptune");




When do we use StringBuilder?

If a string can change or lots of logic and operations in the construction of the string then use StringBuilder. Also if it is only be accessed from a single thread then StringBuilder is good enough.

Example :

public static void ExampleStringBuilder() {

StringBuilder str_build = new StringBuilder("Neptune");



Difference b/w String, StringBuffer, and StringBuilder.

1. String is immutable whereas StringBuffer and StringBuilder are mutable classes.

2. StringBuffer is thread-safe and synchronized whereas StringBuilder is not. That’s why StringBuilder is faster than StringBuffer.

3. String concatenation operator (+) internally uses StringBuffer or StringBuilder class.

4. For String manipulations in a non-multi threaded environment, we should use StringBuilder else use StringBuffer class.

That’s all for a quick roundup of differences between String, StringBuffer, and StringBuilder. 

Related Blogs
How to use wait commands in Selenium WebDriver in Java ?
Author: neptune | 22nd-Feb-2022 | Views: 1612
#Selenium #Testing #Java
We are going to explore different types of waits in Selenium WebDriver. Implicit wait, Explicit wait, and Fluent wait with examples...

25 Basic Java interview questions.
Author: neptune | 30th-May-2022 | Views: 793
#Interview #Java
We will explore 25 basic Java interview questions...

Best way to reverse a String for beginners in Java.
Author: neptune | 02nd-Oct-2022 | Views: 540
We will explore How to reverse a String in Java using simple way. If you are beginner in Java then you are at the right place...

Where you applied OOPs in Automation Testing?
Author: neptune | 11th-Dec-2022 | Views: 524
#Interview #Java
You may face this question Where you have applied OOPs concept in Automation Framework? in almost all the Selenium Interviews. Let's learn OOP’s concept in Java before going further...

How would one explain object-oriented programming to a beginner, using a layman language?
Author: neptune | 29th-Oct-2022 | Views: 308
In 1994 Rolling Stone interview, Steve Jobs explains What is object-oriented programming?..

Static Import Concept in Java
Author: neptune | 31st-Dec-2022 | Views: 135
If you overuse the static import feature, it makes the program unreadable and unmaintainable...

View More