Attic
CollectionsStrings

Overview

Overview of Java Strings, immutable character sequences, builder utilities, and String Constant Pool mechanics

In Java, String is the universal datatype for text representation. Unlike primitive types or raw arrays, String instances are immutable objects backed by the JVM String Constant Pool and optimized under the hood with Compact Strings.


Roadmap

The String chapter is organized from practical API methods and high-performance mutable builders to low-level JVM memory interning:


Topics


Quick Comparison: Text Types in Java

FeatureStringStringBuilderStringBuffer
MutabilityImmutable (all edits yield new objects)Mutable (in-place buffer edits)Mutable (in-place buffer edits)
Thread SafetyInherently thread-safe (read-only)Not thread-safe (fastest)Thread-safe (synchronized methods)
Heap StorageHeap + String Constant PoolHeap onlyHeap only
PerformanceHigh allocation overhead in loopsOptimal for single-threaded string assemblySlower due to lock acquisition overhead
JDK AvailabilityJava 1.0+Java 5+Java 1.0+ (Legacy)
Primary Use CaseFixed text, map keys, domain modelsDynamic concatenation, algorithms, parsersMulti-threaded legacy shared logging

On this page