Interview question
What is the difference between String, StringBuffer, and StringBuilder? String, StringBuffer और StringBuilder में क्या अंतर है?
Answer
| Feature | String | StringBuffer | StringBuilder |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread-Safe | Yes (immutable) | Yes (synchronized) | No (not synchronized) |
| Performance | Slow (new objects) | Moderate | Fast (no sync overhead) |
| Use Case | Single concatenation | Multi-threading | Single-thread performance |
String str = "Hello";
str = str + " World";
StringBuffer sbf = new StringBuffer("Hello");
sbf.append(" World");
StringBuilder sbd = new StringBuilder("Hello");
sbd.append(" World");| Feature | String | StringBuffer | StringBuilder |
|---|---|---|---|
| Mutable? | No (Immutable) | Yes | Yes |
| Thread-Safe? | Yes | Yes (Synchronized) | No |
| Speed | Slow | Medium | Fast |
Was this answer clear?