Compare the Difference Between Similar Terms

Difference Between String StringBuffer and StringBuilder in Java

Key Difference – String vs StringBuffer vs StringBuilder in Java
 

String, StringBuffer and String Builder are classes in Java. String is widely used in Java programming. Once an object of String is created, it is not possible to change them. Each time a change occurs to the String, it creates a new String. Even if it is a concatenation to an existing String it creates a new String. This causes memory wastage. StringBuffer and StringBuilder classes in Java are used to modify String. The key difference between String, StringBuffer and StringBuilder in Java is that String is a class to create an object of type String that is a sequence of characters, StringBuffer is a class that is used to modify Strings that provides thread safety, and StringBuilder is a class that is used to modify Strings that do not provide thread safety.

CONTENTS

1. Overview and Key Difference
2. What is String in Java
3. What is StringBuffer in Java
4. What is StringBuilder in Java
5. Similarities Between String StringBuffer and StringBuilder in Java
6. Side by Side Comparison – String vs StringBuffer vs StringBuilder in Java in Tabular Form
7. Summary

What is String in Java?

String class is in java.lang package. Each time the programmer creates a String, it is an object of type String. Strings are immutable meaning once the object is created, it cannot be changed. Objects created using wrapper classes such as Integer, Byte, Float, Double are also immutable. A string literal is enclosed in double quotes. e.g. “Hello World”.  Each time a string literal created, the Java Virtual Machine (JVM) checks the String constant pool. If the String exists, a reference to the String constant pool is returned. If it is a new String, that object is created in the String constant pool.

Figure 01: Java program using String, StringBuffer and StringBuilder

Refer the below piece of code.

String  s1= “Hello”;

s1 = s1 + “World”;

System.out.println(s1);

In the first statement,  s1 is referring to the “Hello” in the String constant pool. In the second statement, JVM does not change the existing String. Instead, it creates a new String as “Hello World” and s1 is now referring to that new String. The exiting “Hello” object still exists in the String constant pool.

If there is a code which is,

String  s1 =”Hello”;

String s2 = s1;

s1, s2 both will be referring to the String object “Hello”.

What is StringBuffer in Java?

StringBuffer class is used to make String objects mutable. Therefore, those objects can be modified. StringBuffer defines four constructors. StringBuffer(), StringBuffer(int size), StringBuffer(String str), StringBuffer (charSequence [] ch)

Refer the code below,

StringBuffer s1= new StringBuffer(“Hello”);

s1.append(“World”);

System.out.println(s1);

In statement 1, s1 is referring to the “Hello” object in a heap. The object is mutable because it is created using StringBuffer. In statement 2, “World” is appended to the same “Hello” String object.

String objects created with StringBuffer class can save memory. StringBuffer provides thread safety because two threads cannot access the same method in the StringBuffer class simultaneously. Thread safety decrease StringBuffer performance. StringBuffer class contain methods such as append(), insert(), reverse(), replace().

What is StringBuilder in Java?

StringBuilder class is used to make String objects mutable. Therefore, those objects can be modified. The functionality is similar to StringBuffer, but this does not provide thread safety. StringBuilder has constructors such as StringBuilder(), StringBuilder(int size), StringBuilder(String str).

Refer the below code.

StringBuilder s1 =  new StringBuilder(“Hello”);

s1.append(“World”);

System.out.println(s1);

In statement 1, s1 is referring to the “Hello” object in a heap. The object is mutable because it is created using StringBuilder. In statement 2, “World” is appended to the same “Hello” String object.  There is no creation of completely new String object.

String objects created with StringBuilder class can save memory. Unlike in StringBuffer, StringBuilder does not provide thread safety because two threads can access the same method in the StringBuilder class simultaneously. StringBuilder class contain methods such as append(), insert(), reverse(), replace().

What is the Similarity Between String, StringBuffer and StringBuilder in Java?

What is the Difference Between String StringBuffer and StringBuilder in Java?

String vs StringBuffer vs StringBuilder

String The string is a Java class that is used to create an object of type String, which is a sequence of characters.
StringBuffer StringBuffer is a Java class that is used to create String objects, which can be modified with thread safety.
StringBuilder StringBuilder is a class that is used to create string objects, which can be modified without thread safety.
Mutability
String The string is an immutable class.
StringBuffer StringBuffer is a mutable class.
StringBuilder StringBuilder is a mutable class.
Thread Safety
String Methods of String are thread safe.
StringBuffer Methods of StringBuffer are thread-safe and synchronized.
StringBuilder Methods of StringBuilder are not threaded safe and not synchronized.
Performance
String The string is fast.
StringBuffer StringBuffer is slow.
StringBuilder StringBuilder is fast.

Summary – String vs StringBuffer vs StringBuilder in Java 

String, StringBuffer and StringBuilder appear to be the same, but they have different meanings. All of these are Java classes. The difference between String, StringBuffer and StringBuilder in Java is that, String is class to create an object of type String, which is a set of characters, StringBuffer is a class that is used to modify Strings and provide thread safety, while StringBuilder is a class that is used to modify Strings which does not provide thread safety.

Download the PDF String vs StringBuffer vs StringBuilder in Java

You can download PDF version of this article and use it for offline purposes as per citation note. Please download PDF version here Difference Between String StringBuffer and StringBuilder in Java

Reference:

1.“String vs StringBuffer vs StringBuilder.” JournalDev, 30 July 2017. Available here
2.“Java Hungry.” Difference Between String , StringBuilder and StringBuffer Classes with Example : Java | Java Hungry. Available here 
3.tutorialspoint.com. “Java Strings.” The PointAvailable here