Online Tutorials & Training Materials | STechies.com
Register Login

Difference between StringBuffer and String with Comparison Chart

|| 0

Difference between StringBuffer and String with Comparison Chart
Stechies

The following article discusses the most popular Java classes used for handling strings StringBuffer, String and StringBuilder. As strings created in Java cannot be modified, other classes are required for performing operations on them. While developing a large application in Java, a new object is created using the new keyword. Even if there is a cached object of the same value, it is ignored. When creating multiple string objects in an application, the String class may create many temporary objects that may cause unnecessary memory allocation. To handle such situations effectively using the memory space and increasing the speed of the application, the original string might have to be altered.

Stringbuffer vs String

Basis of comparison

StringBuffer

String

Definition

It is a class where the length of the object can be increased.

It is a class where the length of the string object remains fixed.

On the basis of modification

The object here is mutable.

The object here is immutable.

Storage

It uses Heap memory.

It uses the string constant pool for storage.

Speed

It is fast and consumes less memory during concatenation of two or more strings.

It is slower as it uses more memory space while concatenating strings as a new instance is created every time during the process.

Comparison

In the Object class, this does not override the equals() method. So the method cannot be used for comparison of two strings.

This overrides the equals() method so the contents of two strings can be easily compared using the method.

Introduction of String in Java

The String is a library in the Java programming language. This class is encapsulated under the java.lang.String package. Every string that is created in Java actually belongs to the String class and is its object. This library consists of different methods to perform string operations like concatenation, trimming, comparing, converting, replacing string contents, etc. The different methods are:

  • toUpperCase()
  • toLowerCase()
  • trim()
  • charAt()
  • startsWith()
  • endsWith()
  • length()

String objects are immutable and are thread safe. Therefore, the String objects can be shared among multiple threads without synchronization. The String objects cannot be changed once created.

What makes String immutable? Explain with an example.

String objects are stored in the String pool where many clients have access to the contents. There is a risk that modifications made by a client will affect the other clients as well. So for performance issue, the String objects were made immutable by making it final. As strings are a very popular HashMap key they are immutable. This will make the process of vale retrieval easier that is stored in the HashMap.

For example, consider the code

String str = "wisdom";

Here, the string wisdom is stored with a reference of str. Even if new concatenations are performed on it, the original string object will not be affected and a new string object will be returned.

Introduction of String Buffer in Java

The StringBuffer class can be considered as a peer class of the String class that has many functionalities. They represent character sequences that are mutable and have substrings appended at the end or inserted in the middle. The java.lang.StringBuffer extends the Object class.

The StringBuffer class was introduced in JDK 1.0. Unlike string objects where their memory allocation is final and cannot be altered, the objects created through StringBuffer have methods to change them. The objects are thread-safe and most of the methods are synchronized. Therefore, these object cannot be accessed by more than one threads at a time. Due to synchronization, the class may operate slower.

Interfaces like Serializable, CharSequence, and Appendable can be implemented by the StringBuffer class. The AbstractStringBuilder, which is an abstract class is extended by the java.lang.StringBuffer class. The initial size is of 16 characters and the maximum size is 2^31-1.

Characteristics of StringBuffer

The different characteristics of StringBuffer are:

  • The capacity() method is used for obtaining the memory required for new character sequences that are to be added.
  • The class has methods like append(), reverse(), delete(), ensure(), length(), insert() and substring() methods for string operations.
  • Methods like clone(), notifyAll(), equals(), getClass(), finalize(), hashCode() and notify() are inherited from the java.lang.Object class.
  • StringBuffer class is faster than the String class while performing concatenations.
  • A StringBuffer works like a normal string but can be modified in the same location in memory.

What makes StringBuilder mutable? Explain with an example.

Strings in Java are immutable by default and only their copies can be altered. StringBuilder is used to represent a mutable sequence of strings. It will mutate the existing strings. It is mutable because, behind the strings inserted in it, an array of characters is maintained. As it extends the AbstractStringBuilder class, it has the same variables as that of the class. So when a string is passed into StringBuilder, it gets stored as an array. As arrays are always mutable, the StringBuilder is mutable.

For example, if there exists a string “I am an Indian.” and we want to add another string “and I live in Kolkata” to it the insert() method has to be used.

package com.journaldev.java;

public class StringBuilderExample {

    public static void main(String[] args) {

        StringBuilder sb = new StringBuilder("I am an Indian.");
        sb.insert(14, " and I live in Kolkata"); 
        System.out.println(sb);

    }

}

The resultant string will be “I am an Indian and I live in Kolkata” Here the original string will have been modified.

Why StringBuffer is faster than String?

The StringBuffer is faster than String as it does not have to create a new string while modification and alters the existing string with the help of an array. Here the internal array is updated to make changes to the original string, hence reducing the memory allocation requirement and increasing the speed of the process.

How is the StringBuffer implemented in Java?

StringBuffer can be implemented through its different constructors:

  • StringBuffer() – This creates an empty StringBuffer with 16 reserved characters as the default size.
  • StringBuffer(int sizeOfBuffer) – This creates a StringBuffer with an argument that specifies its size.
  • StringBuffer(String string) – This makes a StringBuffer with an argument that specifies the initial string along with 16 memory locations are provided for making changes to the strings.

Conclusion

It has been observed that the classes String, StringBuffer and StringBuilder have certain benefits that help in manipulating data. The StringBuilder is a useful class for modification of immutable characters that is very helpful for programmers while developing applications. As they are safe for being used by multiple threads, the methods can be synchronized as per requirement. This makes all the operations of an instance work as if they are in a serial order. When an operation is performed on a source sequence, the source is not synchronized and the string buffer is synchronized. However, when multiple threads are used StringBuffer must be used instead of StringBuilder.


Related Articles