StringBuilder and StringBuffer
In contrast to the immutable string, the java.lang.StringBuilder class is a modifiable and expandable buffer for characters. You can use it to create a big string efficiently. StringBuilder andStringBuffer are twins; they have exactly the same API. StringBuilder was added in Java 5.0 as a drop-in, unsynchronized replacement for StringBuffer. We’ll come back to that in a bit.
First, let’s look at some examples of String construction:
- // Could be better
- String ball = "Hello";
- ball = ball + " there.";
- ball = ball + " How are you?";
复制代码This example creates an unnecessary String object each time we use the concatenation operator (+). Whether this is significant depends on how often this code is run and how big the string actually gets. Here’s a more extreme example:
- // Bad use of + ...
- while( (line = readLine()) != EOF )
- text += line;
复制代码This example repeatedly produces new String objects. The character array must be copied over and over, which can adversely affect performance. The solution is to use a StringBuilder object and itsappend() method:
- StringBuilder sb = new StringBuilder("Hello");
- sb.append(" there.");
- sb.append(" How are you?");
- StringBuilder text = new StringBuilder();
- while( (line = readline()) != EOF )
- text.append( line );
复制代码
Here, the StringBuilder efficiently handles expanding the array as necessary. We can get a String back from the StringBuilder with its toString() method:
- String message = sb.toString();
复制代码You can also retrieve part of a StringBuilder as a String by using one of the substring() methods.
You might be interested to know that when you write a long expression using string concatenation, the compiler generates code that uses a StringBuilder behind the scenes:
- String foo = "To " + "be " + "or";
复制代码It is really equivalent to:
String foo = new StringBuilder().append("To ").append("be ").append("or").toString();
In this case, the compiler knows what you are trying to do and takes care of it for you.
The StringBuilder class provides a number of overloaded append() methods for adding any type of data to the buffer. StringBuilder also provides a number of overloaded insert() methods for inserting various types of data at a particular location in the string buffer. Furthermore, you can remove a single character or a range of characters with the deleteCharAt() and delete() methods. Finally, you can replace part of the StringBuilder with the contents of a String using the replace() method. The String and StringBuilder classes cooperate so that, in some cases, no copy of the data has to be made; the string data is shared between the objects.
You should use a StringBuilder instead of a String any time you need to keep adding characters to a string; it’s designed to handle such modifications efficiently. You can convert the StringBuilder to a String when you need it, or simply concatenate or print it anywhere you’d use a String.
As we said earlier, StringBuilder was added in Java 5.0 as a replacement for StringBuffer. The only real difference between the two is that the methods of StringBuffer are synchronized and the methods of StringBuilder are not. This means that if you wish to use StringBuilder from multiple threads concurrently, you must synchronize the access yourself (which is easily accomplished). The reason for the change is that most simple usage does not require any synchronization and shouldn’t have to pay the associated penalty (slight as it is).