String vs StringBuilder in Java

In this post, we will learn the difference between String and StringBuilder in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.

In Java, a String is an immutable object, which means once a String object is created, we can't change its value.

StringBuilder, on the other hand, is mutable, meaning we can change the content of the object once it's created.

Difference between String and StringBuilder in Java

String StringBuilder
The String is immutable, meaning once a String object is created, it cannot be changed. Any modification results in a new String object. StringBuilder is mutable, meaning it can be modified after it has been created.
The String concatenation operator (+) internally uses StringBuffer or StringBuilder, but every time a new object gets created. StringBuilder objects are like String objects but they can be modified more efficiently.
Because of immutability, String is thread-safe and can be safely used in a multi-threaded environment. StringBuilder is not thread-safe and cannot be safely used in a multi-threaded environment without external synchronization.
When you need a string to not change its value, then use String class objects. When you need to make a lot of modifications to strings of characters, then use a StringBuilder.
The String is less efficient because each time it is edited, an entirely new instance is created. StringBuilder is more efficient than String when performing simple concatenations.

Example

Let's create an example to illustrate the difference between String and StringBuilder in Java. 

The String is an immutable class, while the StringBuilder is a mutable class. Immutable means that once a String object is created, its value cannot be changed. On the other hand, StringBuilder is designed for situations where frequent modifications to a string are required without creating new objects. 

Example using String:

public class StringExample {
    public static void main(String[] args) {
        String str = "Hello";
        System.out.println("Original String: " + str);

        // Concatenating a new value to the string
        str = str + " World";
        System.out.println("Modified String (New Object): " + str);

        // Performing another operation
        str = str.toUpperCase();
        System.out.println("Modified String (New Object): " + str);
    }
}

Output:

Original String: Hello
Modified String (New Object): Hello World
Modified String (New Object): HELLO WORLD
Example using StringBuilder:

public class StringBuilderExample {
    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder("Hello");
        System.out.println("Original StringBuilder: " + builder);

        // Appending a new value to the StringBuilder
        builder.append(" World");
        System.out.println("Modified StringBuilder: " + builder);

        // Performing another operation
        builder.reverse();
        System.out.println("Modified StringBuilder: " + builder);
    }
}

Output:

Original StringBuilder: Hello
Modified StringBuilder: Hello World
Modified StringBuilder: dlroW olleH
In the StringBuilder example, when we append " World" to the builder using the builder.append(" World"), it modifies the builder in place without creating a new object. Similarly, when we reverse the builder using the builder.reverse(), it also modifies the builder in place. This demonstrates that StringBuilder objects are mutable. 

Download Cheat Sheet:

Conclusion

In summary, the key difference between String and StringBuilder is that String objects are immutable and any modification operations result in new objects, whereas StringBuilder objects are mutable, allowing modifications in place without creating new objects. If you need to perform frequent string manipulations and avoid unnecessary object creation, StringBuilder is the appropriate choice. However, if immutability is desired for thread safety or other reasons, then String is the preferred choice.

References


Comments