String vs StringBuffer vs StringBuilder
String is a special class defined in the java.lang package.
This class represents character strings. Literals in Java programs, such as "abc", are implemented as instances of this class.
For example:
1: String string = "This is the [nothiswordanymorebecauseofveryhighdensity]";
This class includes methods for examining individual characters of the sequence, for comparing, searching, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase.
This class objects are immutable objects. Once it is created, it can not be modified, takes up memory until garbage collected.
Strings can be concatenated using “+” operator or concat() method.
1: public void testStringConcatenation() {
2: String aaa = "aaa";
3: String bbb = "bbb";
4: String result = aaa + bbb;
5: System.out.println(result);
6: String result1 = aaa.concat(bbb);
7: System.out.println(result1);
8:
9: //output:aaabbb
10: // aaabbb
11: }
Although fully legitimate these approaches must be used very carefully and never be used in loops with big amount of iterations.
Instead, either StringBuffer or StringBuilder must be used.
StringBuffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
StringBuffer objects are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.
The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a strings and then appends or inserts the characters of strings to the stringbuffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point.
StringBuilder class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the stringbuffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
Example of using StringBuffer/StringBuilder methods:
1: public void testStringBuilderBuffer(){
2: //StringBuffer builderOrBuffer = new StringBuffer();
3: StringBuilder builderOrBuffer = new StringBuilder();
4: System.out.println(builderOrBuffer.toString());
5: builderOrBuffer.append("aaa").append("bbb");
6: System.out.println(builderOrBuffer.toString());
7: builderOrBuffer.insert(3, "ccc");
8: System.out.println(builderOrBuffer.toString());
9: builderOrBuffer.insert(6, (String)null);
10: System.out.println(builderOrBuffer.toString());
11:
12: //output: aaabbb
13: // aaacccbbb
14: // aaacccnullbbb
15: }
Summary: usage of StringBuilder recommended to concatenate big amount of strings in the loop. And sometimes to use benefits of insert() method.
StringBuffer must be used instead of StringBuilder in case of multithreaded access because its methods are synchronized.