Java String vs StringBuffer

In this section, we will discuss the key differences between String and StringBuffer class. Before moving to the ahead in this section, let’s introduce with both classes.

String Class

The String class represents character strings. All string literals in Java programs, such as “xyz”, are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable, they can be shared.

StringBuffer Class

StringBuffer is a thread-safe, mutable sequence of characters. It is the same as a String. The only difference is that it cannot be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type

Difference Between String and StringBuffer Class

StringStringBuffer
String is an immutable class.StringBuffer is a mutable class.
Concatenation of string is slow in the String class.Concatenation of string is fast in the StringBuffer class.
Contents of the two strings can be compared using the equals() method, as the String class overrides the equals() method of the Object class.Contents of the two strings cannot be compared using the equals() method, as the StringBuffer class does not override the equals() method of the Object class.

Performance Comparison

The following comparison is based on the basis of the concatenation of string for the String class and the StringBuffer class.

FileName: PerformanceTestExample.java

Output:

Explanation: It is evident that the manipulation of a string in the String class is much slower as compared to the StringBuffer class. The reason behind this is the String class is immutable. Therefore, when a string in the String class is manipulated, the current string is not manipulated at all. Instead of manipulating the current string, memory for a new string is allocated, and a new string is generated. All these memory allocation and generation of the new string take time.

Because StringBuffer class is mutable, no new string is generated when the string manipulation is done. The current string is manipulated to get the desired output. Therefore, the manipulation is faster in the StringBuffer class as compared to the String class.

Hash Code Test for String and StringBuffer

We have learnt that when a string is manipulated in the String class, a new string is generated, and the same is not true for the StringBuffer class. Let’s confirm it with the help of an example.

FileName: HashCodeTestExample.java

Output:

Explanation: For the strings of the class String, the hash code is different when the string is manipulated, that shows the immutability of the class String. For the StringBuffer class, the hash code is same even after the manipulation showing that strings are not immutable in the StringBuffer class.

Pin It on Pinterest

Share This