# String vs StringBuilder vs StringBuffer 

In Java, strings are widely used to store and manipulate text. However, Java provides three different classes for handling string-related operations: **String**, **StringBuilder**, and **StringBuffer**.

These classes may appear similar because they all deal with text. But internally they behave very differently in terms of **mutability, performance, and thread safety**.

Understanding when to use each of them is important for writing **efficient and optimized Java code**. In this article, we will explore:

*   What **String**, **StringBuilder**, and **StringBuffer** are
    
*   The key differences between them
    
*   When to use each one in real-world scenarios
    

### 1\. String (Immutable)

The **String** class in Java is **immutable**, which means once a String object is created, its value cannot be changed.

If we try to modify a string, Java actually creates a **new object in memory** instead of modifying the existing one.

Example:

```java
String text = "Hello";
text = text + " World";

System.out.println(text);

Output:
Hello World
```

Here, `"Hello"` is not modified. Instead, Java creates a **new String object** `"Hello World"`.

Because of immutability, Strings are **safe, predictable, and widely used in Java applications**.

### 2\. StringBuffer (Mutable and Thread-Safe)

Unlike String, **StringBuffer is mutable**, meaning its value can be modified without creating a new object.

It is also **thread-safe**, because its methods are synchronized. This makes it safe to use in **multi-threaded environments**.

Example:

```plaintext
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World");

System.out.println(sb);

Output:
Hello World
```

The original object is modified instead of creating a new one.

However, synchronization makes **StringBuffer slightly slower** compared to StringBuilder.

### 3\. StringBuilder (Mutable and Faster)

**StringBuilder** is similar to StringBuffer but **not thread-safe**.

Since it does not use synchronization, it is **faster and more efficient** when working in **single-threaded applications**.

Example:

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

System.out.println(sb);

Output:
Hello World
```

For most modern Java applications where thread safety is not required, **StringBuilder is usually preferred**.

### Quick Comparison

| Feature | String | StringBuffer | StringBuilder |
| --- | --- | --- | --- |
| Mutability | Immutable | Mutable | Mutable |
| Thread Safety | Yes | Yes | No |
| Performance | Slow for modifications | Medium | Fast |
| Use Case | Fixed text | Multi-threaded apps | Single-threaded apps |

* * *

### Conclusion

Choosing the right string class can significantly impact your application's performance.

*   Use **String** when the value will not change.
    
*   Use **StringBuffer** when working with **multi-threaded environments**.
    
*   Use **StringBuilder** when you need **fast string manipulation in single-threaded applications**.
    

Understanding these differences helps developers write **cleaner, faster, and more memory-efficient Java code**.
