ReviseAlgo Logo

Strings

StringBuilder

Examine StringBuilder architecture, dynamic array resizing, buffer allocation profiles, and optimal usage patterns.

Interview: Tests capacity growth calculations, internal buffer copies, and non-thread-safe performance profiles.

Last Updated: June 13, 2026 10 min read

The StringBuilder class is a mutable sequence of characters. Unlike String, it allows modifying the character sequence in-place without generating garbage on the heap. Because it is non-thread-safe (no method synchronization), it offers maximum performance for single-threaded operations.

Core Idea

An unsynchronized, mutable character builder that manages an internal growable byte/char buffer on the heap.

Why It Matters

Pre-allocating buffer capacity eliminates expensive resizing copy loops during large string builds.

Interview Lens

Focuses on capacity growth formulas, array resizing operations, and performance compared to StringBuffer.

Resizing Mechanics

A StringBuilder maintains a backing array. By default, it initializes with a capacity of 16 characters.

When elements exceed the current capacity, the builder resizes by allocating a new array and copying the content over:

// JVM internal resizing formula
newCapacity = (oldCapacity * 2) + 2;

This array allocation and copying operation is expensive. If you know the approximate size of the final string, passing the capacity to the constructor (e.g. new StringBuilder(5000)) avoids these resizing steps.

Common Pitfalls

  • Default Capacity Waste: Building large strings without setting an initial capacity, forcing the JVM to perform frequent resize and memory copy operations.
  • Sharing across threads: Using StringBuilder as a shared field in multi-threaded environments, leading to data corruption since operations are not synchronized.
  • Converting to String early: Calling toString() inside a loop, creating unnecessary intermediate string objects.

Best Practices

  • Always provide an initial capacity estimate when initializing a StringBuilder: new StringBuilder(expectedLength).
  • Use StringBuilder for string manipulations confined to local method scopes.
  • Chain calls (e.g., sb.append(a).append(b)) to write clean, readable code.

Interview-Relevant Information

Q1: Why is StringBuilder faster than StringBuffer?
Answer: StringBuilder's methods are not synchronized, eliminating lock acquisition and thread synchronization overhead. This makes it much faster for local method operations.

Q2: What is the capacity growth formula of StringBuilder?
Answer: When the buffer is full, StringBuilder allocates a new array of size (oldCapacity * 2) + 2, copies the elements using System.arraycopy(), and discards the old array.

Quick Checklist

Can you trace capacity resizing, configure initial capacities, explain thread-safety differences, and optimize GC pressure? If yes, you understand StringBuilder.

Use Cases

Building dynamic SQL query strings locally in data repository classes.

Assembling HTML layouts dynamically from collection entities in web view rendering engines.

Common Mistakes

Using default constructor capacities for assembling large string structures, causing frequent array copy loops.

Sharing a StringBuilder instance across threads without external synchronization.