Use StringBuffer Instead of String in Java



In Java, both String and StringBuffer classes are used to represent sequences of characters. However, the String class is immutable, which means once a String object is created, its value cannot be changed, while the StringBuffer class is mutable. It allows us to modify the contents of the string without creating a new object.

In this article, we will discuss why we should use the StringBuffer class instead of String in Java.

Why Use StringBuffer Instead of String?

A few reasons why StringBuffer is often preferred over String is given below:

  • A StringBuffer is thread-safe because its methods are synchronized. We can use it when multiple threads are modifying the same buffer.
  • Unlike a String class, the StringBuffer class represents a mutable sequence of characters. Therefore, all modification operations like appending, inserting, or deleting happen on the same object.
  • We can also use StringBuffer when repeated string concatenations inside loops are required. It provides better performance since it avoids creating multiple intermediate String objects.
  • The StringBuffer class offers a different set of methods than the String class, all of which operate directly on the buffer that contains the string. Some of its methods are: append(), insert(), replace(), and delete().
  • A StringBuffer can be defined simply by the use of the new operator and passing the string value inside a StringBuffer constructor.

Example: Disadvantages of String

The System.identityHashCode() returns a unique memory reference for the String object. If the hash codes before and after modification do not match, that means a new String object was created instead of modifying the original one. In the following example, we are checking whether the same String is being modified or not.

Open Compiler
public class StringDemo { public static void main(String[] args) { String str = "Java Tutorials Point"; System.out.println("Original String: " + str); System.out.println("Original String HashCode: " + System.identityHashCode(str)); // Modify the string str = "Tutorialspoint Java"; System.out.println("New String: " + str); System.out.println("New String HashCode: " + System.identityHashCode(str)); } }

On running the above Java program, you will get two different HashCodes as shown below:

Original String: Java Tutorials Point
Original String HashCode: 2038148563
New String: Tutorialspoint Java
New String HashCode: 2008966511

Example: Using StringBuffer in Java

In the below Java program, we initially created an instance of the StringBuffer class and appended a string to the StringBuffer class using the append() method. It will modify the same object rather than creating a new one.

Open Compiler
public class StringBufferDemo { public static void main(String arg[]){ StringBuffer sb = new StringBuffer(); sb.append("Java Tutorials Point"); System.out.println(sb); } }

Output of the above code is as follows:

Java Tutorials Point
Updated on: 2025-05-19T19:59:59+05:30

886 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements