String vs StringBuffer vs StringBuilder

April 26, 2011 | String and StringBuffer
Three classes exist in java.lang package to manipulate strings - String, StringBuffer and StringBuilder. Even though all the three are used for manipulating strings only but they differ in many aspects and specifically used in the code where demanded. Following tables give the differences between these and when to use. Now let us tabulate the differences of String vs StringBuffer vs StringBuilder at a glance to know better.

Note: For a program on length() and capacity() methods refer, "StringBuffer - length & Capacity".

Length vs Capacity

Property

String

StringBuffer

Length

The length() gives the number of characters stored in the location.

length() method also exist that gives the number of characters stored in the buffer

Capacity

Capacity concept does not exist as no buffer is maintained.

capacity() method returns the storage capacity of the buffer. Default is 16.

Methods

Includes only length() method.

Includes both length() and capacity() methods.

String vs StringBuffer

Property

String

StringBuffer

Immutability

String is immutable. If a value is reassigned to a string, the new value is stored in a new location. As the old value does not have any reference, it is garbage collected.

StringBuffer is mutable. That is, if a new value is assigned, the new value is stored in the same location.

Buffer

Does not have any buffer concept.

A buffer is assigned within which manipulation of string can be done.

Performance

Very very low due to immutability. But gives high performance only when the string is not getting changed in the whole program.

High performance due to non-immutability.

Methods

Does not have string manipulation methods like appending, deleting etc.

Comes with a plethora of methods like append, delete, replace etc.

Reversing

This class does not contain any reversing capability.

Includes reverse() method which reverses the whole string.

Object creation

Object is created implicitly when created as "String s1 = "hello";".

Object should be created explicitly as "StringBuffer sb1 = new StringBuffer();".

Usage

Use when the string is not going to change in the code.

Use when the string is getting changed very often.

StringBuffer vs StringBuilder

StringBuilder was introduced with JDK 1.5. StringBuffer and StringBuilder are conceptually (maintaining buffer) same and includes same methods but for synchronization.

Property

StringBuffer

StringBuilder

Introduction

Introduced with JDK 1.0, the starting version of Java.

Introduced with JDK 1.5 version.

Synchronization

All methods are synchronized.

Methods are not synchronized

Usage

Used in multi threaded environment to avoid data inconsistency.

Used in single task programs where no multiple threads access.

Performance

Very low-performance due to synchronized methods.

Very high-performance due to non-synchronized methods.

Programmer goes to StringBuilder mainly for two operations - append() and insert() methods. To accept any data type, these two methods are overloaded.

Following code gives the usage.

public class SBDemo
{
  public static void main(String args[])
  {
    StringBuilder sb = new StringBuilder();

    sb.append("Hello Sir");
    sb.append("Hello World");
    sb.append(100);
    sb.append(false);

    System.out.println(sb);
  }
}

The code looks as if that of StringBuffer. Same methods exist in both with the same functionality but for synchronization. You can use insert() method also.

Note: For a program on StringBuffer insert() method refer, "StringBuffer - Searching, Appending and Inserting".

The other two classes to manipulate strings are StringTokenizer and Character.

View All for Java Differences on 90 Topics