0% found this document useful (0 votes)
2 views6 pages

StringBuilder Class in Java - GeeksforGeeks

The StringBuilder class in Java provides a mutable sequence of characters, allowing efficient string manipulation without creating new objects, making it faster than String. It offers various methods for string operations and is preferred over StringBuffer in single-threaded applications due to its lack of synchronization. Key features include constructors for different use cases, methods for appending, inserting, and deleting characters, and it is not thread-safe, making it unsuitable for multi-threaded environments.

Uploaded by

yitomek452
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

StringBuilder Class in Java - GeeksforGeeks

The StringBuilder class in Java provides a mutable sequence of characters, allowing efficient string manipulation without creating new objects, making it faster than String. It offers various methods for string operations and is preferred over StringBuffer in single-threaded applications due to its lack of synchronization. Key features include constructors for different use cases, methods for appending, inserting, and deleting characters, and it is not thread-safe, making it unsuitable for multi-threaded environments.

Uploaded by

yitomek452
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

StringBuilder Class in Java

Last Updated : 8 May, 2026

In Java, the StringBuilder class (part of the [Link] package) provides a mutable sequence of
characters. Unlike the String class (which is immutable), StringBuilder allows modification of
character sequences without creating new objects, making it memory-efficient and faster for
frequent string operations.

It provides similar functionality to StringBuffer, but without thread safety.


StringBuilder is not synchronized, so it performs better in single-threaded applications.
Use StringBuffer only when thread safety is required; otherwise, prefer StringBuilder for
improved performance.

Declaration:

StringBuilder sb = new StringBuilder("Initial String");

Example:

public class Geeks {


public static void main(String[] args)
{
StringBuilder sb
= new StringBuilder("GeeksforGeeks");
[Link]("Initial StringBuilder: " + sb);

[Link](" is awesome!");
[Link]("After append: " + sb);
}
}

Output

Initial StringBuilder: GeeksforGeeks


After append: GeeksforGeeks is awesome!

Explanation : This example demonstrates the use of the append() method in StringBuilder to add
text at the end of an existing string.

The append() method adds the given string to the end of the existing sequence without
creating a new object. This makes StringBuilder efficient for concatenation operations.

Hierarchy of StringBuilder in Java


It extends the AbstractStringBuilder class and implements the CharSequence and Serializable
interfaces.

StringBuilder Class

StringBuilder Constructors
StringBuilder class provides multiple constructors for different use cases.

StringBuilder() : Creates an empty builder with a default capacity of 16 characters.


StringBuilder(int capacity) : Creates an empty builder with a specified initial capacity.
StringBuilder(String str) : Initializes the builder with the content of the given String.
StringBuilder(CharSequence cs) : Initializes the builder with the given CharSequence (for
example, String or StringBuffer).

Example:

public class StringBuilderConstructorsDemo {


public static void main(String[] args)
{
StringBuilder sb1 = new StringBuilder();
[Link]("Hello");
[Link]("sb1: " + sb1);

StringBuilder sb2 = new StringBuilder(50);


[Link]("This has initial capacity 50");
[Link]("sb2: " + sb2);

StringBuilder sb3 = new StringBuilder("Geeks");


[Link]("ForGeeks");
[Link]("sb3: " + sb3);

CharSequence cs = "Java";
StringBuilder sb4 = new StringBuilder(cs);
[Link]("Programming");
[Link]("sb4: " + sb4);
}
}

Output

sb1: Hello
sb2: This has initial capacity 50
sb3: GeeksForGeeks
sb4: JavaProgramming

Explanation : This program demonstrates different constructors of the StringBuilder class and
how to append data using the append() method.

Commonly Used Methods in StringBuilder


The StringBuilder class provides various methods for string manipulation.

Example: Using StringBuilder Methods

public class Geeks {


public static void main(String[] args) {
StringBuilder sb = new StringBuilder("GeeksforGeeks");
[Link]("Initial: " + sb);

[Link](" is awesome!");
[Link]("After append: " + sb);
[Link](13, " Java");
[Link]("After insert: " + sb);

[Link](0, 5, "Welcome to");


[Link]("After replace: " + sb);

[Link](8, 14);
[Link]("After delete: " + sb);

[Link]();
[Link]("After reverse: " + sb);

[Link]("Capacity: " + [Link]());


[Link]("Length: " + [Link]());

char c = [Link](5);
[Link]("Character at index 5: " + c);

[Link](5, 'X');
[Link]("After setCharAt: " + sb);

String sub = [Link](5, 10);


[Link]("Substring (5–10): " + sub);

[Link](); // Revert for search


[Link]("Index of 'Geeks': " + [Link]("Geeks"));

[Link](5);
[Link]("After deleteCharAt: " + sb);

String result = [Link]();


[Link]("Final String: " + result);
}
}

Output:

Explanation : This program demonstrates various StringBuilder methods such as append(),


insert(), replace(), delete(), reverse(), substring(), and setCharAt() to perform dynamic string
manipulation efficiently in Java.

Important StringBuilder Methods


Method Description Example

append(String str) Appends the specified string to the end of


[Link]("Geeks");
the StringBuilder.

insert(int offset, String) Inserts the specified string at the given


[Link](5, " Geeks");
position in the StringBuilder.

replace(int start, int Replaces characters in a substring with


[Link](6, 11, "Geeks");
end, String) the specified string.

delete(int start, int end) Removes characters in the specified


[Link](5, 11);
range.

reverse() Reverses the sequence of characters in


[Link]();
the StringBuilder.

capacity() Returns the current capacity of the


int cap = [Link]();
StringBuilder.

length() Returns the number of characters in the


int len = [Link]();
StringBuilder.

charAt(int index) Returns the character at the specified


char ch = [Link](4);
index.

setCharAt(int index, Replaces the character at the specified


[Link](0, 'G');
char) position with a new character.

substring(int start, int Returns a new String that contains String sub = [Link](0,
end) characters from the specified range. 5);

ensureCapacity(int Ensures the capacity of the StringBuilder


[Link](50);
minimum) is at least equal to the specified minimum.

deleteCharAt(int index) Removes the character at the specified


[Link](3);
position.

indexOf(String str) Returns the index of the first occurrence int idx =
of the specified string. [Link]("Geeks");
Method Description Example

lastIndexOf(String str) Returns the index of the last occurrence of int idx =
the specified string. [Link]("Geeks");

toString() Converts the StringBuilder object to a


String result = [Link]();
String.

StringBuilder vs String vs StringBuffer


The table below demonstrates the difference between String, StringBuilder and StringBuffer:

Features String StringBuilder StringBuffer

Mutability String are StringBuilder are


StringBuffer are mutable
immutable(creates new mutable(modifies in
(modifies in place)
objects on modification) place)

Thread-Safe It is thread-safe It is not thread-safe It is thread-safe

Performance It is slow because it It is slower due to


It is faster (no object
creates an object each synchronization
creation)
time overhead

Use Case Fixed, unchanging Single-threaded string Multi-threaded string


strings manipulation manipulation

Advantages of StringBuilder
Performs faster string manipulations in single-threaded environments.
Reduces memory overhead by modifying content in place.
Automatically increases capacity when needed.
Suitable for operations inside loops where strings are frequently changed.

Limitations of StringBuilder
Not thread-safe because it is not synchronized.
May consume extra memory if large capacity is allocated unnecessarily.
Requires additional synchronization for safe use in multi-threaded applications.

You might also like