StringBuffer Vs StringBuilder
Table of contents
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:
- 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.
- 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.
- When to use StringBuffer or StringBuilder:
Use StringBuffer when thread safety is required.
Use StringBuilder when thread safety is not required.
- 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.
- 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:
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!
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
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
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
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!
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
Subscribe to my newsletter
Read articles from Sudarshan Doiphode directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Sudarshan Doiphode
Sudarshan Doiphode
๐ Hey there! I'm Sudarshan, a tech enthusiast with a focus on: โข ๐ Spring Boot โข ๐ผ Spring โข ๐ Spring Security โข ๐ฑ Hibernate โข โ Java 8 & Java โข ๐ Apache Kafka โข ๐ข๏ธ Apache Maven โข ๐ณ Docker โข ๐ Git โข ๐๏ธ Design Patterns โข And more tech adventures! Follow for insights and join the journey. ๐๐จโ๐ป #TechEnthusiast