StringBuffer Vs StringBuilder

StringBuffer Vs StringBuilder

Table of contents

No heading

No headings in the article.

From an Interview point of view, StringBuilder and StringBuffer are the most important topics that everyone should learn in Java.

A) Key Points of StringBuffer and StringBuilder:

  1. What are StringBuffer and StringBuilder:
  • Both are classes in Java used for manipulating string objects.

  • StringBuffer and StringBuilder both are used to create mutable string objects.

  • As String is Immutable we can use StringBuffer and StringBuilder to create mutable String objects.

  1. Differences between StringBuffer and StringBuilder:
  • StringBuffer is thread-safe, while StringBuilder is not.

  • StringBuffer is synchronized, while StringBuilder is not.

  • StringBuilder is faster than StringBuffer in non-concurrent environments.

  1. When to use StringBuffer or StringBuilder:
  • Use StringBuffer when thread safety is required.

  • Use StringBuilder when thread safety is not required.

  1. Common methods of StringBuffer and StringBuilder:
  • append(): appends a string to the end of the buffer.

  • insert(): inserts a string at a specified position in the buffer.

  • delete(): removes a sequence of characters from the buffer.

  • replace(): replaces a sequence of characters with another sequence.

  • These 4 methods are used, a lot of time by developers.

  1. Performance considerations:
  • StringBuffer and StringBuilder are faster than concatenating strings using the "+" operator, especially when dealing with large strings.

  • StringBuilder is faster than StringBuffer in non-concurrent environments, but the performance difference is negligible for small strings.

  • Let me explain this with one example:

    Example 1:
    String s = "Hello";

    for (int i = 0; i < 10000; i++) {

    s += "world";

    }

    In this example, the "+" operator is used to concatenate "world" to the string "s" 10,000 times. This creates a new string object every time, resulting in poor performance. Using StringBuffer or StringBuilder instead would be much faster:

    Example 2:
    StringBuffer sb = new StringBuffer("Hello");

    for (int i = 0; i < 10000; i++) {

    sb.append("world");

    }

    String s = sb.toString();

    In this example, StringBuffer is used to append "world" to the buffer 10,000 times, and then toString() is called to convert the buffer to a string. This creates only one new string object, resulting in better performance.

  • Best practices:

  • Use StringBuffer or StringBuilder appropriately depending on the requirements of your application.

  • Be mindful of the performance implications when manipulating large strings.

B) Here are some examples of StringBuffer and StringBuilder:

  1. Creating an empty StringBuffer and appending strings to it:

    StringBuffer sb = new StringBuffer();

    sb.append("Hello");

    sb.append(" ");

    sb.append("world!");

    System.out.println(sb.toString()); // Output: Hello world!

  1. Creating a StringBuilder and inserting a string at a specific position:

    StringBuilder sb = new StringBuilder("Hello world");

    sb.insert(5, ",");

    System.out.println(sb.toString()); // Output: Hello, world

  1. Creating a StringBuffer and deleting a sequence of characters:

    StringBuffer sb = new StringBuffer("Hello world");

    sb.delete(5, 11);

    System.out.println(sb.toString()); // Output: Hello

  1. Creating a StringBuilder and replacing a sequence of characters:

    StringBuilder sb = new StringBuilder("Hello world");

    sb.replace(6, 11, "Java");

    System.out.println(sb.toString()); // Output: Hello Java

  1. Concatenating strings using StringBuffer:

    String s1 = "Hello";

    String s2 = "world!";

    StringBuffer sb = new StringBuffer();

    sb.append(s1).append(" ").append(s2);

    System.out.println(sb.toString()); // Output: Hello world!

  1. Concatenating strings using StringBuilder:

    String s1 = "Hello";

    String s2 = "world!";

    StringBuilder sb = new StringBuilder();

    sb.append(s1).append(" ").append(s2);

    System.out.println(sb.toString()); // Output: Hello world!

Here are some more common methods of StringBuffer and StringBuilder:

  • capacity(): returns the current capacity of the buffer.

  • length(): returns the length (number of characters) of the buffer.

  • reverse(): reverses the sequence of characters in the buffer.

  • toString(): returns the contents of the buffer as a string.

Here are the code examples:

StringBuffer sb = new StringBuffer("hello");

System.out.println(sb.capacity()); // Output: 21 (default capacity is 16 + length of "hello")

System.out.println(sb.length()); // Output: 5

sb.reverse();

System.out.println(sb.toString()); // Output: olleh