Fundamentals 16 min read

Understanding Java String, StringBuilder, and StringBuffer

This article explains the immutable nature of Java's String class, compares it with mutable StringBuilder and thread‑safe StringBuffer, shows how the JVM handles string literals, constant pool, intern(), and provides performance guidelines and code examples for efficient string manipulation.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding Java String, StringBuilder, and StringBuffer

Java strings are represented by the String class, which is immutable and stored as a char[] array. Once created, a String cannot be modified; any operation that appears to change it actually creates a new object.

The JVM keeps string literals in a constant pool. When a literal is first encountered, it is placed in the pool; subsequent uses reference the same object, reducing memory usage. The intern() method can explicitly add a string to the pool, but its behavior changed after JDK 1.7 when the pool moved from the permanent generation to the heap.

For mutable sequences, Java provides StringBuilder and StringBuffer . Both extend AbstractStringBuilder and store characters in a resizable char[] called value with a count of used characters.

StringBuilder is not synchronized, making it suitable for single‑threaded scenarios and offering better performance. StringBuffer adds synchronized blocks to its methods, ensuring thread safety at the cost of overhead.

When the + operator is used, the compiler translates it into new StringBuilder().append(...).toString() . In simple concatenations with constant literals, the compiler can pre‑compute the result, creating only one object. In loops, each iteration creates a new StringBuilder , which can be avoided by reusing a single builder and calling append() inside the loop.

Both mutable classes grow automatically. Their initial capacity is 16 characters, and when more space is needed, the internal array is expanded roughly to (oldCapacity << 1) + 2 . If this new capacity is still insufficient, it is increased to the exact required size.

Key methods of String include charAt , substring , indexOf , equals , concat , replace , split , and trim . StringBuilder and StringBuffer share methods such as append , insert , delete , and toString , and both implement Serializable and CharSequence .

In summary, use String for immutable text, StringBuilder for fast, single‑threaded concatenation, and StringBuffer when thread safety is required. Be aware of the constant pool and object creation patterns to write memory‑efficient Java code.

JavaperformancestringimmutabilityStringBuilderStringBuffer
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.