← Back to Core Java Course | Chapter 6: Strings | Lesson 3 of 5

Java StringBuilder & StringBuffer

StringBuilder is a text holder you can change over and over without making a new one each time, like an eraser-friendly whiteboard instead of a printed page. StringBuffer is the safe-for-many-users version.
Syntax
java
StringBuilder builder = new StringBuilder();
builder.append(value);
String result = builder.toString();

Introduction to StringBuilder

StringBuilder builds a mutable sequence of characters that can be changed in place, which makes it dramatically faster than repeated String concatenation inside a loop — each += on a String silently creates a whole new String object, while StringBuilder.append() just extends an internal buffer.

Example: Introduction to StringBuilder

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new StringBuilder object and store it in sb
		StringBuilder sb = new StringBuilder();
		sb.append("Hello");
		sb.append(" World");
		// Print a line to the console
		System.out.println(sb);
	}
}

Inserting and Deleting

Because StringBuilder is mutable, you can insert characters at any position with insert(index, value) or remove a range with delete(start, end) without creating a new object each time, unlike the equivalent operations on an immutable String.

Example: Inserting and Deleting

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new StringBuilder object and store it in sb
		StringBuilder sb = new StringBuilder("Hello World");
		sb.insert(5, ",");
		// Print a line to the console
		System.out.println(sb);
		sb.delete(0, 6);
		// Print a line to the console
		System.out.println(sb);
	}
}

Reversing with StringBuilder

reverse() flips the entire character sequence in place, which is a frequent building block in coding interview problems like palindrome checks or digit-reversal puzzles — it's far simpler than manually swapping characters from both ends.

Example: Reversing with StringBuilder

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new StringBuilder object and store it in sb
		StringBuilder sb = new StringBuilder("Hello");
		sb.reverse();
		// Print a line to the console
		System.out.println(sb);
	}
}

Checking Capacity and Length

length() reports how many characters are currently stored, while capacity() reports how much internal buffer space is allocated before the builder needs to grow and reallocate — capacity is always greater than or equal to length, and you rarely need to think about it unless you're optimizing for performance.

Example: Checking Capacity and Length

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new StringBuilder object and store it in sb
		StringBuilder sb = new StringBuilder("Hello");
		// Print a line to the console
		System.out.println(sb.length());
		// Print a line to the console
		System.out.println(sb.capacity());
	}
}

Thread Safe StringBuffer

StringBuffer has an identical API to StringBuilder but synchronizes its methods, making it safe to share across multiple threads at the cost of extra locking overhead.

Use StringBuilder by default for single-threaded code, and reach for StringBuffer only when you actually need that thread safety.

Example: Thread Safe StringBuffer

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new StringBuffer object and store it in sb
		StringBuffer sb = new StringBuffer();
		sb.append("Hello");
		sb.append(" World");
		// Print a line to the console
		System.out.println(sb);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using String += in a loop instead of StringBuilder, which creates many temporary objects.
  2. Comparing StringBuilder objects with equals(), which compares references and not text; compare toString().
  3. Forgetting to call toString() when a String is needed.
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.