What is some real time situation to compare String, StringBuffer, and StringBuilder?
12 Answers
Mutability Difference:
String is immutable. If you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable, so they can change their values.
Thread-Safety Difference:
The difference between StringBuffer and StringBuilder is that StringBuffer is threadsafe. So when the application needs to be run only in a single thread, then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer.
Situations:
- If your string is not going to change use a String class, because a
Stringobject is immutable. - If your string can change (example: lots of logic and operations in the construction of the string) and will only be accessed from a single thread, using a
StringBuilderis good enough. - If your string can change, and will be accessed from multiple threads, use a
StringBufferbecauseStringBufferis synchronous so you have thread-safety.
5 Comments
Strings when we alter the value another object is created. Does the old object reference is nullified so that it may be garbage collected by the GC or is it even garbage collected?String with StringBuilder?- You use
Stringwhen an immutable structure is appropriate; obtaining a new character sequence from aStringmay carry an unacceptable performance penalty, either in CPU time or memory (obtaining substrings is CPU efficient because the data is not copied, but this means a potentially much larger amount of data may remain allocated). - You use
StringBuilderwhen you need to create a mutable character sequence, usually to concatenate several character sequences together. - You use
StringBufferin the same circumstances you would useStringBuilder, but when changes to the underlying string must be synchronized (because several threads are reading/modifyind the string buffer).
See an example here.
2 Comments
The Basics:
String is an immutable class; it can't be changed.
StringBuilder is a mutable class that can be appended to, characters replaced or removed and ultimately converted to a String
StringBuffer is the original synchronized version of StringBuilder
You should prefer StringBuilder in all cases where you have only a single thread accessing your object.
The Details:
Also note that StringBuilder/Buffers aren't magic. They just use an Array as a backing object and that Array has to be reallocated whenever it gets full. Be sure and create your StringBuilder/Buffer objects large enough originally where they don't have to be constantly resized every time .append() gets called.
The resizing can get very degenerate. It basically resizes the backing Array to two times its current size every time it needs to be expanded. This can result in large amounts of RAM getting allocated and not used when StringBuilder/Buffer classes start to grow large.
In Java, String x = "A" + "B"; uses a StringBuilder behind the scenes. So for simple cases there isn't any benefit of declaring your own. But if you are building String objects that are large, say less than 4k, then declaring StringBuilder sb = StringBuilder(4096); is much more efficient than concatenation or using the default constructor which is only 16 characters. If your String is going to be less than 10k then initialize it with the constructor to 10k to be safe. But if it is initialize to 10k then you write one character more than 10k, it will get reallocated and copied to a 20k array. So initializing high is better than to low.
In the auto resize case, at the 17th character the backing Array gets reallocated and copied to 32 characters, at the 33th character this happens again and you get to reallocated and copy the Array into 64 characters. You can see how this degenerates to lots of re-allocations and copies which is what you really are trying to avoid using StringBuilder/Buffer in the first place.
This is from the JDK 6 source code for AbstractStringBuilder:
void expandCapacity(int minimumCapacity) {
int newCapacity = (value.length + 1) * 2;
if (newCapacity < 0) {
newCapacity = Integer.MAX_VALUE;
} else if (minimumCapacity > newCapacity) {
newCapacity = minimumCapacity;
}
value = Arrays.copyOf(value, newCapacity);
}
A best practice is to initialize the StringBuilder/Buffer a little bit larger than you think you are going to need if you don't know right off hand how big the String will be, but you can guess. One allocation of slightly more memory than you need is going to be better than lots of reallocations and copies.
Also beware of initializing a StringBuilder/Buffer with a String as that will only allocated the size of the String + 16 characters, which in most cases will just start the degenerate reallocation and copy cycle that you are trying to avoid. The following is straight from the Java 6 source code.
public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}
If you by chance do end up with an instance of StringBuilder/Buffer that you didn't create and can't control the constructor that is called, there is a way to avoid the degenerate reallocate and copy behavior. Call .ensureCapacity() with the size you want to ensure your resulting String will fit into.
The Alternatives:
Just as a note, if you are doing really heavy String building and manipulation, there is a much more performance-oriented alternative called Ropes.
Another alternative, is to create a StringList implementation by subclassing ArrayList<String>, and adding counters to track the number of characters on every .append() and other mutation operations of the list, then override .toString() to create a StringBuilder of the exact size you need and loop through the list and build the output, you can even make that StringBuilder an instance variable and 'cache' the results of .toString() and only have to regenerate it when something changes.
Also don't forget about String.format() when building fixed formatted output, which can be optimized by the compiler as they make it better.
3 Comments
String x = "A" + "B"; really compile to be a StringBuilder? Why wouldn't it just compile to String x = "AB";, it should only use a StringBuilder if the components aren't known at compile time.You may mean for concatenation.
Real-world example: You want to create a new string out of many others.
For instance, to send a message:
String version:
String s = "Dear " + user.name + "<br>" +
" I saw your profile and got interested in you.<br>" +
" I'm " + user.age + "yrs. old too"
StringBuilder version:
String s = new StringBuilder().append.("Dear ").append(user.name).append("<br>")
.append(" I saw your profile and got interested in you.<br>")
.append(" I'm ").append(user.age).append("yrs. old too")
.toString()
or
String s = new StringBuilder(100).appe..... etc. ...
// The difference is a size of 100 will be allocated
// upfront as fuzzy lollipop points out.
The StringBuffer version looks like the StringBuilder but the effects differ.
About
StringBuffer vs. StringBuilder
The former is synchronized and later is not.
So, if you invoke it several times in a single thread (which is 90% of the cases), StringBuilder will run much faster, because it won't stop to see if it owns the thread lock.
So, it is recommendable to use StringBuilder (unless of course you have more than one thread accessing to it at the same time, which is rare).
String concatenation (using the + operator) may be optimized by the compiler to use StringBuilder underneath, so, it is not longer something to worry about. In the elder days of Java, this was something that everyone says should be avoided at all cost, because every concatenation created a new String object. Modern compilers don't do this anymore, but still it is a good practice to use StringBuilder instead just in case you use an "old" compiler.
Just for those who are curious, this is what the compiler does for this class:
class StringConcatenation {
int x;
String literal = "Value is" + x;
String builder = new StringBuilder().append("Value is").append(x).toString();
}
javap -c StringConcatenation
Output:
Compiled from "StringConcatenation.java"
class StringConcatenation extends java.lang.Object{
int x;
java.lang.String literal;
java.lang.String builder;
StringConcatenation();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: aload_0
5: new #2; //class java/lang/StringBuilder
8: dup
9: invokespecial #3; //Method java/lang/StringBuilder."<init>":()V
12: ldc #4; //String Value is
14: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
17: aload_0
18: getfield #6; //Field x:I
21: invokevirtual #7; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
24: invokevirtual #8; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
27: putfield #9; //Field literal:Ljava/lang/String;
30: aload_0
31: new #2; //class java/lang/StringBuilder
34: dup
35: invokespecial #3; //Method java/lang/StringBuilder."<init>":()V
38: ldc #4; //String Value is
40: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
43: aload_0
44: getfield #6; //Field x:I
47: invokevirtual #7; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
50: invokevirtual #8; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
53: putfield #10; //Field builder:Ljava/lang/String;
56: return
}
Lines numbered 5 - 27 are for the String named "literal"
Lines numbered 31-53 are for the String named "builder".
There isn't any difference; exactly the same code is executed for both strings.
5 Comments
StringBuilder to do String concatenation on the right-hand side of the assignment. Any good implementation will use a StringBuilder behind the scenes as you say. Furthermore, your example of "a" + "b" would be compiled into a single literal "ab" but if you used StringBuilder it would result in two needless calls to append()."a"+"b" but to say what was a String concatenation about I change it to be explicit. What you don't say is, why is not a good practice to do it. That's exactly what ( a modern ) compiler does. @fuzzy, I agree, specially when you know what the size of the final string would be ( aprox ) .String
The String class represents character strings. All string literals in Java program, such as "abc" are implemented as instances of this class.
String objects are immutable once they are created we can't change. (Strings are constants)
If a String is created using constructor or method then those strings will be stored in Heap Memory as well as
SringConstantPool. But before saving in pool it invokesintern()method to check object availability with same content in pool using equals method. If String-copy is available in the Pool then returns the reference. Otherwise, String object is added to the pool and returns the reference.- The Java language provides special support for the string concatenation operator (
+), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.
String heapSCP = new String("Yash"); heapSCP.concat("."); heapSCP = heapSCP + "M"; heapSCP = heapSCP + 777; // For Example: String Source Code public String concat(String str) { int otherLen = str.length(); if (otherLen == 0) { return this; } int len = value.length; char buf[] = Arrays.copyOf(value, len + otherLen); str.getChars(buf, len); return new String(buf, true); }- The Java language provides special support for the string concatenation operator (
String literals are stored in
StringConstantPool.String onlyPool = "Yash";
StringBuilder and StringBuffer are mutable sequence of characters. That means one can change the value of these object's. StringBuffer has the same methods as the StringBuilder, but each method in StringBuffer is synchronized so it is thread safe.
StringBuffer and StringBuilder data can only be created using new operator. So, they get stored in Heap memory.
Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.
StringBuffer threadSafe = new StringBuffer("Yash"); threadSafe.append(".M"); threadSafe.toString(); StringBuilder nonSync = new StringBuilder("Yash"); nonSync.append(".M"); nonSync.toString();StringBuffer and StringBuilder are having a Special methods like.,
replace(int start, int end, String str)andreverse().NOTE: StringBuffer and SringBuilder are mutable as they provides the implementation of
Appendable Interface.
When to use which one.
If a you are not going to change the value every time then its better to Use
String Class. As part of Generics if you want to SortComparable<T>or compare a values then go forString Class.//ClassCastException: java.lang.StringBuffer cannot be cast to java.lang.Comparable Set<StringBuffer> set = new TreeSet<StringBuffer>(); set.add( threadSafe ); System.out.println("Set : "+ set);If you are going to modify the value every time the go for StringBuilder which is faster than StringBuffer. If multiple threads are modifying the value the go for StringBuffer.
Comments
-----------------------------------------------------------------
String StringBuffer StringBuilder
-----------------------------------------------------------------
Storage area | Constant string pool Heap Heap
Modifiable | No (immutable) Yes (mutable) Yes (mutable)
Thread Safe | Yes Yes No
Performance | Fast Very slow Fast
-----------------------------------------------------------------
3 Comments
synchronised and that's the why.Note that if you are using Java 5 or newer, you should use StringBuilder instead of StringBuffer. From the API documentation:
As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread,
StringBuilder. TheStringBuilderclass should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
In practice, you will almost never use this from multiple threads at the same time, so the synchronization that StringBuffer does is almost always unnecessary overhead.
Comments
Personally, I don't think there is any real-world use for StringBuffer. When would I ever want to communicate between multiple threads by manipulating a character sequence? That doesn't sound useful at all, but maybe I have yet to see the light :)
1 Comment
The difference between String and the other two classes is that String is immutable and the other two are mutable classes.
But why do we have two classes for the same purpose?
The reason is that StringBuffer is threadsafe and StringBuilder is not.
StringBuilder is a new class in the StringBuffer` API and it was introduced in JDK 5 and is always recommended if you are working in a singlethreaded environment as it is much faster.
For complete details, you can read http://www.codingeek.com/java/stringbuilder-and-stringbuffer-a-way-to-create-mutable-strings-in-java/
Comments
In Java, String is immutable. Being immutable we mean that once a String is created, we can not change its value.
StringBuffer is mutable. Once a StringBuffer object is created, we just append the content to the value of object instead of creating a new object.
StringBuilder is similar to StringBuffer, but it is not thread-safe. Methods of StingBuilder are not synchronized, but in comparison to other Strings, the Stringbuilder runs the fastest.
You can learn the difference between String, StringBuilder and StringBuffer by implementing them.
Comments
Here is a small code snippet illustrating how you can break StringBuilder in an asynchronous environment:
public static void main(String[] args) throws InterruptedException {
StringBuilder builder = new StringBuilder();
ExecutorService executorService = Executors.newFixedThreadPool(50);
for (int i = 0; i < 1000 * 1000; i++) {
executorService.submit(new AppendRunnable(builder));
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
Stream.of(builder.toString().split(System.lineSeparator()))
.filter(line -> !line.equals("I just appended this!"))
.forEach(System.out::println);
}
record AppendRunnable(StringBuilder stringBuilder) implements Runnable {
@Override
public void run() {
stringBuilder.append("I just appended this!\n");
}
}
Essentially, we append strings to the builder and we expect that all of them are equal to "I just appended this!". But they are not; some of them are prefixed with null characters, because the internal character buffer of the builder is not synchronized and it gets wrongly resized. Using StringBuffer in this situation solves the problem. More detailed information is here.
