227

What is some real time situation to compare String, StringBuffer, and StringBuilder?

0

12 Answers 12

401

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 String object 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 StringBuilder is good enough.
  • If your string can change, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous so you have thread-safety.
5
  • 18
    Also, using String for logic operations is rather slow, and is not advised at all, since the JVM converts the String to a StringBuffer in the bytecode. A lot of overhead is wasted converting from String to StringBuffer and then back to String again. Commented Jun 4, 2010 at 8:28
  • 2
    So in 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? Commented May 9, 2014 at 7:07
  • @PietervanNiekerk What do you mean with logic operations?
    – Emil Laine
    Commented Sep 29, 2015 at 10:38
  • logic operations i mean are basic String ones, Now one thing I would like to ask, as stated by @Peter should we start using StringBuffer instead on String in all cases or there are some specific cases?
    – JavaDragon
    Commented Jun 17, 2016 at 7:57
  • @bakkal Can I use all the methods of String with StringBuilder? Commented Oct 20, 2016 at 17:33
49
  • You use String when an immutable structure is appropriate; obtaining a new character sequence from a String may 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 StringBuilder when you need to create a mutable character sequence, usually to concatenate several character sequences together.
  • You use StringBuffer in the same circumstances you would use StringBuilder, but when changes to the underlying string must be synchronized (because several threads are reading/modifyind the string buffer).

See an example here.

2
  • 2
    concise, but incomplete, it misses the fundamental reason to use StringBuilder/Buffer and that is to reduce or eliminate the re-allocation and array copy of the regular String concatenation behavior.
    – user177800
    Commented Jun 4, 2010 at 4:21
  • 1
    "You use String when you are dealing with immutable strings" - doesn't make sense. Instances of String ARE immutable, so maybe the comment should read "Use String when the memory usage due to immutability doesn't matter". The accepted answer covers it's bases pretty well.
    – fooMonster
    Commented Aug 3, 2011 at 14:24
29

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
  • 1
    Does 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.
    – Matt Greer
    Commented Jun 4, 2010 at 3:58
  • it might optimize the String constants out, I can't recall from the last time decompiled the byte code, but I do know that if there are any variables in there it will use a StringBuilder implementation for sure. You can download the JDK source code and find out yourself. "A" + "B" is a contrived example for sure.
    – user177800
    Commented Jun 4, 2010 at 4:06
  • I was wondering about String.format(). I haven't ever really seen it being used in projects. Usually it's the StringBuilder. Well, usually it's actually "A" + "B" + "C" because people are lazy ;) I tended to always use a StringBuilder, even if it was only two strings being concatenated, because in future, perhaps more strings would be appended. I never used String.format() mainly because I never remembered what JDK it was introduced - I see it's JDK1.5, I'd use it in favour of the other options. Commented Oct 8, 2010 at 10:37
10

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
  • 2
    this is a really bad example. Initializing the StringBuilder with " Dear " means the first .append() will cause a reallocation and copy. Completely negating any efficiency that you are trying to gain over "normal" concatenation. A better example would to be to create it with a initial size that will hold the entire contents of the final String.
    – user177800
    Commented Jun 4, 2010 at 3:48
  • 1
    It is NOT generally a good practice to use a 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(). Commented Jun 4, 2010 at 4:18
  • @Mark I didn't mean to use "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 ) .
    – OscarRyz
    Commented Jun 4, 2010 at 16:40
  • 1
    I don't think it's particularly bad practice, but I certainly wouldn't want any recommendation to do that in general. It's clunky and hard to read, and overly verbose. Plus, it encourages mistakes like yours where you are breaking up two literals that could otherwise be compiled as one. Only if profiling told me it made a difference would I do it your way. Commented Jun 4, 2010 at 16:58
  • @Mark Got it. I was thinking more in the "big chunk of code like a template" and not really in every regular string literal. But yes, I agree, since they do the same thing nowadays, it doesn't make sense ( 10 yrs ago was a reason to reject a change in a code revision ) :)
    – OscarRyz
    Commented Jun 4, 2010 at 17:03
8

String Family

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 invokes intern() 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);
    }
    
  • 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) and reverse().

    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 Sort Comparable<T> or compare a values then go for String 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.

8
-----------------------------------------------------------------
               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
  • Why String performance is fast and StringBuffer performance very slow?
    – Gaurav
    Commented Oct 30, 2018 at 11:00
  • 1
    @gaurav you can read its source code, all methods in StringBuffer is synchronised and that's the why.
    – Hearen
    Commented May 4, 2019 at 6:29
  • Storage Area of String depends on How we are creating the String, let's say- Approach-1: String str1= "ABC" - The value will be stored in SCP.(it is inside Heap Memory) Approach-2: String str1= new String("ABC")- The Object will be created in Heap memory. Commented Sep 15, 2020 at 6:37
4

Also, StringBuffer is thread-safe, which StringBuilder is not.

So in a real-time situation when different threads are accessing it, StringBuilder could have an undeterministic result.

3

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. The StringBuilder class 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.

3

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
  • That is a good point. It seems to be a contradiction in terms. Commented Apr 15, 2024 at 12:31
3

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/

2

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.

0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.