We already know that a String is used to store text in Java. For example:
String name = "John";
But there is one important characteristic of Strings that we need to understand: Strings are immutable.
That means once a String is created, its original value cannot be changed. If we perform an operation that appears to modify a String, Java actually creates a new String.
This is perfectly fine when we are working with small amounts of text. But if we need to modify text many times, creating a large number of new String objects can be inefficient.
That's where StringBuilder becomes useful.
What is StringBuilder?
StringBuilder is a Java class used to create and modify a sequence of characters.
Unlike a String, a StringBuilder can be changed after it is created. We can add text, remove text, replace characters, and modify the content without creating a completely new object for every small change.
For example:
StringBuilder name = new StringBuilder("John");
Now we can modify it:
name.append(" Smith");
If we print it:
System.out.println(name);
the output will be:
John Smith
Adding Text with append()
The most commonly used StringBuilder method is append().
It adds text to the end of the existing content.
StringBuilder message = new StringBuilder("Hello");
message.append(" ");
message.append("John");
System.out.println(message);
Output:
Hello John
We can also append numbers and other values:
StringBuilder result = new StringBuilder("Score: ");
result.append(95);
System.out.println(result);
Output:
Score: 95
This makes StringBuilder useful when we need to build a String piece by piece.
Inserting Text
We can insert text at a specific position using insert().
StringBuilder name = new StringBuilder("John");
name.insert(4, " Smith");
System.out.println(name);
Output:
John Smith
Here, 4 is the position where we want to insert the text.
Removing Text
We can remove characters using the delete() method.
StringBuilder text = new StringBuilder("Hello World");
text.delete(5, 11);
System.out.println(text);
Output:
Hello
The delete() method removes characters from the starting index up to, but not including, the ending index.
Replacing Text
We can replace part of the content using replace().
StringBuilder text = new StringBuilder("Hello John");
text.replace(6, 10, "Jason");
System.out.println(text);
Output:
Hello Jason
This replaces the characters from index 6 to 9 with "Jason".
Reversing a StringBuilder
StringBuilder also provides a convenient reverse() method.
StringBuilder text = new StringBuilder("Java");
text.reverse();
System.out.println(text);
Output:
avaJ
This can be useful when solving certain programming problems involving strings.
Converting StringBuilder to String
Sometimes we finish building our text and need an actual String.
We can use toString():
StringBuilder builder = new StringBuilder("Hello John");
String message = builder.toString();
System.out.println(message);
Now message is a normal String.
This is useful when a method or API expects a String instead of a StringBuilder.
String vs StringBuilder
The main difference is simple:
String | StringBuilder |
|---|---|
Immutable | Mutable |
Cannot be changed after creation | Can be modified |
Good for normal text | Useful when repeatedly modifying text |
Operations may create new String objects | Designed for modifying existing content |
For example, if we repeatedly add text to a String:
String text = "";
text += "Hello ";
text += "John ";
text += "Welcome!";
each modification creates a new String because Strings are immutable.
With StringBuilder, we can do:
StringBuilder text = new StringBuilder();
text.append("Hello ");
text.append("John ");
text.append("Welcome!");
This is generally more suitable when we are building or modifying text repeatedly.
A Simple Example
Let's say we want to build a message step by step:
class Main {
public static void main(String[] args) {
StringBuilder message = new StringBuilder();
message.append("Hello, ");
message.append("John");
message.append("! Welcome to Java.");
System.out.println(message);
}
}
Output:
Hello, John! Welcome to Java.
The main idea is very simple: use String when you mainly need to store text, and consider StringBuilder when you need to modify or build text repeatedly.