Interview #113: Java: Difference between StringBuffer and StringBuilder?
Software Testing Studio | WhatsApp 91-9606623245
Looking for Job change? WhatsApp 91-9606623245
In Java, both StringBuffer and StringBuilder are mutable alternatives to the String class. While String objects are immutable, meaning they cannot be changed after creation, StringBuffer and StringBuilder allow modifications without creating new objects.
Disclaimer: For QA-Testing Jobs, WhatsApp us @ 91-9606623245
However, there are key differences between StringBuffer and StringBuilder, primarily in terms of thread-safety and performance.
1. Introduction to StringBuffer and StringBuilder
StringBuffer (Thread-Safe, Synchronized)
StringBuilder (Not Thread-Safe, Faster)
2. Key Differences Between StringBuffer and StringBuilder
3. Code Examples and Performance Comparison
Example 1: Using StringBuffer (Thread-Safe)
public class StringBufferExample {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World");
System.out.println(sb); // Output: Hello World
}
}
Example 2: Using StringBuilder (Faster, Not Thread-Safe)
public class StringBuilderExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb); // Output: Hello World
}
}
Example 3: Multi-threaded Scenario
Let's see how StringBuffer is thread-safe while StringBuilder is not.
Using StringBuffer in Multi-threading
class StringBufferThread extends Thread {
static StringBuffer sb = new StringBuffer("Hello");
public void run() {
sb.append(" World");
System.out.println(sb);
}
public static void main(String[] args) {
StringBufferThread t1 = new StringBufferThread();
StringBufferThread t2 = new StringBufferThread();
t1.start();
t2.start();
}
}
Output (Thread-safe behavior):
Hello World
Hello World
Using StringBuilder in Multi-threading (Not Safe)
class StringBuilderThread extends Thread {
static StringBuilder sb = new StringBuilder("Hello");
public void run() {
sb.append(" World");
System.out.println(sb);
}
public static void main(String[] args) {
StringBuilderThread t1 = new StringBuilderThread();
StringBuilderThread t2 = new StringBuilderThread();
t1.start();
t2.start();
}
}
Output (Inconsistent, due to lack of synchronization):
Hello World
Hello World World
4. Performance Comparison: Which One is Faster?
Since StringBuilder is not synchronized, it is faster than StringBuffer in single-threaded applications.
Benchmark Test
public class StringPerformanceTest {
public static void main(String[] args) {
long startTime, endTime;
// Testing StringBuffer Performance
StringBuffer sb = new StringBuffer("Test");
startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
sb.append("A");
}
endTime = System.nanoTime();
System.out.println("StringBuffer Time: " + (endTime - startTime) + " ns");
// Testing StringBuilder Performance
StringBuilder sb2 = new StringBuilder("Test");
startTime = System.nanoTime();
for (int i = 0; i < 100000; i++) {
sb2.append("A");
}
endTime = System.nanoTime();
System.out.println("StringBuilder Time: " + (endTime - startTime) + " ns");
}
}
Expected Result:
5. When to Use StringBuffer vs. StringBuilder?
? Use StringBuffer when:
? Use StringBuilder when:
6. Conclusion
Sr. Software Automation Engineer || SDET || Selenium || Java || C# || Cucumber BDD || Agile || TestNG || NUnit || Maven|| POM || WebdriverIO || Rest API Testing || Appium || JMeter || Git || JIRA || CSPO || CSM || Docker
4 天前Well put