In this post, we will learn the difference between String and StringBuffer in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.
Example using StringBuffer:
String | StringBuffer |
---|---|
The String is immutable, meaning once a String object is created, it cannot be changed. Any modification results in a new String object. | StringBuffer 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. | StringBuffer 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. | StringBuffer is thread-safe and can be safely used in a multi-threaded environment. |
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, especially in a multithreaded context, then use a StringBuffer. |
The String is less efficient because each time it is edited, an entirely new instance is created. | StringBuffer is more efficient than String when performing simple concatenations, particularly in a loop or where synchronization is required. |
Example
Let's create an example to illustrate the difference between String and StringBuffer in Java.
The String is an immutable class in Java, which means that once a String object is created, its value cannot be changed. Any operation that appears to modify a String actually creates a new String object.
On the other hand, StringBuffer is a mutable class that can be used to perform various operations on strings 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
In the String example, when we concatenate a new value to the str using str = str + " World";, it creates a new String object with the modified value. Similarly, when we convert the string to uppercase using str.toUpperCase();, it also creates a new String object with the uppercase value. This demonstrates that String objects are immutable.
public class StringBufferExample {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer("Hello");
System.out.println("Original StringBuffer: " + buffer);
// Appending a new value to the StringBuffer
buffer.append(" World");
System.out.println("Modified StringBuffer: " + buffer);
// Performing another operation
buffer.reverse();
System.out.println("Modified StringBuffer: " + buffer);
}
}
Output:
Original StringBuffer: Hello
Modified StringBuffer: Hello World
Modified StringBuffer: dlroW olleH
In the StringBuffer example, when we append " World" to the buffer using buffer.append(" World"), it modifies the buffer in place without creating a new object. Similarly, when we reverse the buffer using buffer.reverse(), it also modifies the buffer in place. This demonstrates that StringBuffer objects are mutable.
In summary, the key difference between String and StringBuffer is that String objects are immutable and any modification operations result in new objects, whereas StringBuffer objects are mutable, allowing modifications in place without creating new objects. If you need to perform frequent string manipulations, especially in a multi-threaded environment, StringBuffer (or StringBuilder, which is similar to StringBuffer but not thread-safe) is a more efficient choice. If immutability is desired, then String is the appropriate choice.
References
Interview Questions
Java
String
StringBuffer
X vs Y
Comments
Post a Comment