MediumJava String: All about it. Strings are one of the most widely used… | by Mohit Pal | Medium
Sitemap
Mohit Pal

Mohit Pal

Tech Apprentice @ Adobe

Java String: All about it

4 min readJul 4, 2026

--

Press enter or click to view image in full size

Strings are one of the most widely used concepts in Java programming. Whether you are manipulating data, logging information, or building user interfaces, you will inevitably work with strings. However, beneath their simple appearance lies a sophisticated memory management system and a carefully designed architecture.

In this article, we will explore the core concepts of Java Strings, including immutability, memory storage, and the mutable alternatives: StringBuffer and StringBuilder.

Java Strings are objects that are backed by a char array and are immutable. The class java.lang.String is used to create a string object.

Why are Java Strings Immutable?

In Java, once a String object is created, its data or state cannot be changed. If you try to modify it, a new String object is created instead. Java intentionally makes strings immutable for several crucial reasons:

  1. Security: Strings are frequently used to store sensitive information like usernames, passwords, and database connections. Immutability ensures that once a value is validated, it cannot be maliciously altered.
  2. Performance Improvement: Because strings are immutable, they can be heavily cached and reused by the JVM.
  3. Optimize Memory Usage: Immutability allows Java to safely share the same string instances across multiple references in a special memory area (the String Pool).
  4. Thread Safety: Immutable objects are inherently thread-safe. Multiple threads can access a string concurrently without the risk of data corruption or the need for explicit synchronization.

What Happens if We Try to Modify a String?
Let’s look at a practical example:

public static void main(String[] args) {
String s1 = "java";

// Assign the new string to a variable
String s2 = s1.concat(" rules");

// Now s2 refers to "java rules"
System.out.println("s1 refers to " + s1); // Output: java
System.out.println("s2 refers to " + s2); // Output: java rules
}

As shown, calling concat() on s1 does not change s1. Instead, it creates a brand new string s2.

How are Strings Stored?
How are Strings Stored?

How are Strings Stored?

All Strings in Java are stored inside the Heap Memory, but they are managed in two distinct ways depending on how they are created:

Get Mohit Pal’s stories in your inbox

Join Medium for free to get updates from this writer.

1. String Constant Pool (String Pool)
It is a separate space inside the Heap Memory. When you create a string using a literal (e.g., String str1 = “Hello”;), the JVM checks the String Constant Pool inside the heap.

  • Exists? If the string already exists in the pool, the JVM simply returns a reference to the same object.
  • No? If it does not exist, the JVM creates a new instance in the pool.
class StringStorage {

public static void main(Stringll args)
{
// String created using String literal
String s1 = "TAT" ;
String s2 = "TAT";

// String created using 'new' keyword
String s3 = new String (original:"TAT");
String s4 = new String(original:"TAT");

System.out-println(s1);
System.out-println(s2);
System.out-println(s3);
System.out.println(s4);
}
}

2. Heap Memory (Outside the Pool)
When you use the new keyword (e.g., String s3 = new String(“TAT”);), Java bypasses the pool check for object creation. It explicitly creates a new instance in the general Heap memory, even if an identical string literal already exists in the String Pool.

JVM String Allocation Visualization

Handling Mutable Strings: StringBuffer & StringBuilder

Because creating a new object every time a string is modified can degrade performance, Java provides two mutable classes for heavy string manipulation:

StringBuffer Class

  • Mutable: Can grow and shrink as needed.
  • Synchronized: Thread-safe, making it reliable in multi-threaded environments.
  • Example:
StringBuffer sb = new StringBuffer("Java");
sb.append(" Programming");
System.out.println(sb); // Output: Java Programming

StringBuilder Class

  • Mutable: Similar to StringBuffer but not synchronized.
  • Performance: Because it lacks thread safety (no synchronization overhead), it offers significantly higher performance.
  • Example:
StringBuilder sb = new StringBuilder("Java");
sb.append(" Programming");
System.out.println(sb); // Output: Java Programming

Real-Life Use Cases

Choosing between String, StringBuffer, and StringBuilder depends on your application’s specific needs:

  • String: Best for constant, unchanging text.
  • Examples: E-commerce labels (“Add to cart”, “Out of stock”), transaction statuses (“Transaction successful”, “Invalid PIN”), or fixed UI buttons (“Like”, “Comment”, “Share”).
  • StringBuffer: Best when multiple threads need to safely write to the same sequence of characters.
  • Examples: Shared chat histories, Jenkins CI/CD logs of jobs/tasks, or a banking system logging deposit and withdrawal threads concurrently.
  • StringBuilder: Best for rapid string concatenation in a single-threaded scenario.
  • Examples: Single-threaded operations, personal direct chat messages, or real-time in-game indicators and notifications.

Comparison Summary

String               | StringBuffer              | StringBuilder
-------------------- | --------------------------| --------------------------
Immutable | Mutable | Mutable
Thread-safe | Thread-safe | Not thread-safe
Slower | Moderate | Faster
Use for fixed values | Use in multi-threaded env | Use in single-threaded env

Connect With Me: LinkedIn, GitHub

--

--