Skip to main content

Command Palette

Search for a command to run...

StringBuilder in Java – Functions and Capacity Explained Clearly

Published
4 min read

What is StringBuilder?

StringBuilder is a mutable class in Java used to create and modify strings efficiently.
Unlike String, it allows changes to the same object without creating new ones.


Why StringBuilder is Important

  • Faster than String

  • Uses less memory

  • Ideal for loops and dynamic string creation


Important StringBuilder Functions (Methods)

Below are the most commonly used and important methods, explained one by one.


1. append() – Add Content (MOST IMPORTANT)

What it does

Adds data to the end of the StringBuilder.

Example

StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
sb.append(100);

System.out.println(sb);

Output

Hello World100

Why it matters

  • Modifies the same object

  • No new object creation

  • Very fast

✔ Best choice for loops


2. insert() – Insert at a Specific Position

What it does

Inserts text at a given index.

Example

StringBuilder sb = new StringBuilder("HelloWorld");
sb.insert(5, " ");

System.out.println(sb);

Output

Hello World

3. delete() – Remove a Range of Characters

What it does

Deletes characters between two indexes.

Syntax

delete(startIndex, endIndex)

endIndex is exclusive

Example

StringBuilder sb = new StringBuilder("HelloWorld");
sb.delete(5, 10);

System.out.println(sb);

Output

Hello

4. deleteCharAt() – Remove One Character

What it does

Deletes a single character at a given index.

Example

StringBuilder sb = new StringBuilder("Javaa");
sb.deleteCharAt(4);

System.out.println(sb);

Output

Java

5. replace() – Replace Part of the String

What it does

Replaces a range of characters with new text.

Example

StringBuilder sb = new StringBuilder("HelloWorld");
sb.replace(5, 10, "Java");

System.out.println(sb);

Output

HelloJava

6. reverse() – Reverse the String

What it does

Reverses the entire sequence of characters.

Example

StringBuilder sb = new StringBuilder("abcd");
sb.reverse();

System.out.println(sb);

Output

dcba

Interview Use

  • Palindrome checking

  • String manipulation problems


7. length() – Current Length

What it does

Returns the number of characters currently stored.

Example

StringBuilder sb = new StringBuilder("Hello");
System.out.println(sb.length());

Output

5

8. charAt() – Read a Character

What it does

Returns the character at a given index.

Example

StringBuilder sb = new StringBuilder("Java");
System.out.println(sb.charAt(1));

Output

a

9. setCharAt() – Modify a Character

What it does

Changes a character at a specific index.

Example

StringBuilder sb = new StringBuilder("Java");
sb.setCharAt(0, 'L');

System.out.println(sb);

Output

Lava

10. substring() – Extract Part of String (Tricky)

What it does

Returns a portion of the string.

Important Point ⚠️

  • Return type is String, not StringBuilder

Example

StringBuilder sb = new StringBuilder("HelloWorld");
String s = sb.substring(0, 5);

System.out.println(s);

Output

Hello

StringBuilder Capacity (VERY IMPORTANT)

What is Capacity?

Capacity is the size of the internal character buffer (char[]) used by StringBuilder.

  • Length → actual characters stored

  • Capacity → how many characters can be stored without resizing


Default Capacity

StringBuilder sb = new StringBuilder();
System.out.println(sb.capacity());

Output

16

✔ Default internal buffer size = 16


Capacity with Initial String

StringBuilder sb = new StringBuilder("Java");
System.out.println(sb.capacity());

Capacity Calculation

capacity = 16 + length of string
         = 16 + 4
         = 20

How Capacity Grows Internally

When capacity is exceeded, Java creates a bigger buffer using this formula:

newCapacity = oldCapacity × 2 + 2

Example

  • Old capacity = 16

  • New capacity = 16 × 2 + 2 = 34

This reduces frequent memory allocation and improves performance.


ensureCapacity() – Performance Optimization

What it does

Pre-allocates memory to avoid repeated resizing.

Example

StringBuilder sb = new StringBuilder();
sb.ensureCapacity(100);

When to Use

  • When you know large data will be appended

  • Improves speed in heavy string operations