StringBuffer
时间: 2023-11-07 10:56:54 浏览: 38
StringBuffer is a class in Java that represents a mutable sequence of characters. It is similar to the String class, but allows modifications to the content of the sequence.
StringBuffer is designed to be efficient for string concatenation operations. When you concatenate strings using the '+' operator, Java creates a new String object for each concatenation, which can be inefficient if you're doing a lot of concatenation. StringBuffer provides a more efficient way of concatenating strings, by allowing you to append new characters to the end of the buffer without creating new objects each time.
StringBuffer is thread-safe, which means that it can be safely used in multi-threaded environments without the risk of data corruption. It provides a set of synchronized methods that can be used to access and modify the buffer.
Some of the commonly used methods of the StringBuffer class include append(), insert(), delete(), replace(), and reverse(). These methods allow you to modify the content of the buffer in various ways.
Here is an example of how to use StringBuffer to concatenate strings:
```
StringBuffer sb = new StringBuffer();
sb.append("Hello");
sb.append(" ");
sb.append("world");
String result = sb.toString(); // result = "Hello world"
```