Skip To Contentb
Skip To Contentb
geeksforgeeks
Courses 90% Refund
Tutorials
Java
Practice
Contests
Sign In
Java Arrays
Java Strings
Java OOPs
Java Collection
Java 8 Tutorial
Java Multithreading
Java Exception Handling
Java Programs
Java Project
Java Collections Interview
Java Interview Questions
Java MCQs
Spring
Spring MVC
Spring Boot
Hibernate
String
StringBuilder
StringBuffer
Introduction
Mutability
Immutable
Mutable
Mutable
Thread Safety
Thread Safe
Thread Safe
Memory Efficiency
High
Efficient
Less Efficient
Performance
High(No-Synchronization)
High(No-Synchronization)
Low(Due to Synchronization)
Usage
Now, we will be justifying. Let us do consider the below code with three
concatenation functions with three different types of parameters, String,
StringBuffer, and StringBuilder. Let us clear out the understanding between them
via a single Java program below from which we will be drawing out conclusions from
the output generated, to figure out differences between String vs StringBuilder vs
StringBuffer in Java.
Example
// Main class
class GFG {
// Method 1
// Concatenates to String
public static void concat1(String s1)
{
s1 = s1 + "forgeeks";
}
// Method 2
// Concatenates to StringBuilder
public static void concat2(StringBuilder s2)
{
s2.append("forgeeks");
}
// Method 3
// Concatenates to StringBuffer
public static void concat3(StringBuffer s3)
{
s3.append("forgeeks");
}
// Method 4
// Main driver method
public static void main(String[] args)
{
// Custom input string
// String 1
String s1 = "Geeks";
// s1 is not changed
System.out.println("String: " + s1);
// String 1
StringBuilder s2 = new StringBuilder("Geeks");
// s2 is changed
System.out.println("StringBuilder: " + s2);
// String 3
StringBuffer s3 = new StringBuffer("Geeks");
// s3 is changed
System.out.println("StringBuffer: " + s3);
}
}
Output
String: Geeks
StringBuilder: Geeksforgeeks
StringBuffer: Geeksforgeeks
Output explanation:
Note: Geeks now you must be wondering when to use which one, do refer below as
follows:
If a string is going to remain constant throughout the program, then use the String
class object because a String object is immutable.
If a string can change (for example: lots of logic and operations in the
construction of the string) and will only be accessed from a single thread, using a
StringBuilder is good enough.
If a string can change and will be accessed from multiple threads, use a
StringBuffer because StringBuffer is synchronous, so you have thread-safety.
If you don’t want thread-safety than you can also go with StringBuilder class as it
is not synchronized.
Conversion between types of strings in Java
Sometimes there is a need for converting a string object of different classes like
String, StringBuffer, StringBuilder to one another. Below are some techniques to do
the same. Let us do cover all use cases s follows:
Example
// Main class
public class GFG {
Output
skeeG
GeeksForGeeks
Case 2: From StringBuffer and StringBuilder to String
This conversion can be performed using toString() method which is overridden in
both StringBuffer and StringBuilder classes.
Below is the java program to demonstrate the same. Note that while we use
toString() method, a new String object(in Heap area) is allocated and initialized
to the character sequence currently represented by the StringBuffer object, which
means the subsequent changes to the StringBuffer object do not affect the contents
of the String object.
Example
// Main class
public class GFG {
Output
StringBuffer object to String :
Geeks
StringBuilder object to String :
Hello
GeeksForGeeks
Geeks
Case 3: From StringBuffer to StringBuilder or vice-versa
This conversion is tricky. There is no direct way to convert the same. In this
case, We can use a String class object. We first convert the
StringBuffer/StringBuilder object to String using toString() method and then from
String to StringBuilder/StringBuffer using constructors.
Example
// Main class
public class GFG {
Output
Geeks
From the above three use-cases we can conclude out below pointers:
Objects of String are immutable, and objects of StringBuffer and StringBuilder are
mutable.
StringBuffer and StringBuilder are similar, but StringBuilder is faster and
preferred over StringBuffer for the single-threaded program. If thread safety is
needed, then StringBuffer is used.
Related Article: Reverse a String in Java (5 Different Ways)
Three 90 Challenge is back on popular demand! After processing refunds worth INR
1CR+, we are back with the offer if you missed it the first time. Get 90% course
fee refund in 90 days. Avail now!
Want to be a master in Backend Development with Java for building robust and
scalable applications? Enroll in Java Backend and Development Live Course by
GeeksforGeeks to get your hands dirty with Backend Programming. Master the key Java
concepts, server-side programming, database integration, and more through hands-on
experiences and live projects. Are you new to Backend development or want to be a
Java Pro? This course equips you with all you need for building high-performance,
heavy-loaded backend systems in Java. Ready to take your Java Backend skills to the
next level? Enroll now and take your development career to sky highs.