What is the difference between String, StringBuilder and StringBuffer?
| Point | String | StringBuilder | StringBuffer |
|---|---|---|---|
| Changeable | No (immutable) | Yes | Yes |
| Thread safe | Yes (immutable) | No | Yes (synchronized) |
| Speed | Slow for many joins | Fastest | Slower than StringBuilder |
| Use for | Fixed text | Building text in a loop | Old code with many threads |
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Java");
sb.append(" is").append(" fun");
System.out.println(sb);
System.out.println(sb.reverse());
}
}
Output
Java is fun
nuf si avaJ
Details: Java StringBuilder and Text Blocks.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |