Difference between StringBuilder and StringBuffer

后端 未结 30 2232
独厮守ぢ
独厮守ぢ 2020-11-21 15:06

What is the main difference between StringBuffer and StringBuilder? Is there any performance issues when deciding on any one of these?

相关标签:
30条回答
  • 2020-11-21 15:42

    String-Builder :

    int one = 1;
    String color = "red";
    StringBuilder sb = new StringBuilder();
    sb.append("One=").append(one).append(", Color=").append(color).append('\n');
    System.out.print(sb);
    // Prints "One=1, Colour=red" followed by an ASCII newline.
    

    String-Buffer

    StringBuffer sBuffer = new StringBuffer("test");
    sBuffer.append(" String Buffer");
    System.out.println(sBuffer);  
    

    It is recommended to use StringBuilder whenever possible because it is faster than StringBuffer. However, if the thread safety is necessary, the best option is StringBuffer objects.

    0 讨论(0)
  • 2020-11-21 15:43

    A String is an immutable object which means the value cannot be changed whereas StringBuffer is mutable.

    The StringBuffer is Synchronized hence thread-safe whereas StringBuilder is not and suitable for only single-threaded instances.

    0 讨论(0)
  • 2020-11-21 15:46

    Check the internals of synchronized append method of StringBuffer and non-synchronized append method of StringBuilder.

    StringBuffer:

    public StringBuffer(String str) {
        super(str.length() + 16);
        append(str);
    }
    
    public synchronized StringBuffer append(Object obj) {
        super.append(String.valueOf(obj));
        return this;
    }
    
    public synchronized StringBuffer append(String str) {
        super.append(str);
        return this;
    }
    

    StringBuilder:

    public StringBuilder(String str) {
        super(str.length() + 16);
        append(str);
    }
    
    public StringBuilder append(Object obj) {
        return append(String.valueOf(obj));
    }
    
    public StringBuilder append(String str) {
        super.append(str);
        return this;
    }
    

    Since append is synchronized, StringBuffer has performance overhead compared to StrinbBuilder in multi-threading scenario. As long as you are not sharing buffer among multiple threads, use StringBuilder, which is fast due to absence of synchronized in append methods.

    0 讨论(0)
  • 2020-11-21 15:50

    StringBuffer:

    • Multi-Thread
    • Synchronized
    • Slow than StringBuilder

    StringBuilder

    • Single-Thread
    • Not-Synchronized
    • Faster than ever String
    0 讨论(0)
  • 2020-11-21 15:52

    First lets see the similarities: Both StringBuilder and StringBuffer are mutable. That means you can change the content of them, with in the same location.

    Differences: StringBuffer is mutable and synchronized as well. Where as StringBuilder is mutable but not synchronized by default.

    Meaning of synchronized (synchronization): When some thing is synchronized, then multiple threads can access, and modify it with out any problem or side effect. StringBuffer is synchronized, so you can use it with multiple threads with out any problem.

    Which one to use when? StringBuilder : When you need a string, which can be modifiable, and only one thread is accessing and modifying it. StringBuffer : When you need a string, which can be modifiable, and multiple threads are accessing and modifying it.

    Note : Don't use StringBuffer unnecessarily, i.e., don't use it if only one thread is modifying and accessing it because it has lot of locking and unlocking code for synchronization which will unnecessarily take up CPU time. Don't use locks unless it is required.

    0 讨论(0)
  • 2020-11-21 15:52

    Pretty Good Question

    Here are the differences, i have noticed :

    StringBuffer :-

    StringBuffer is  synchronized
    StringBuffer is  thread-safe
    StringBuffer is  slow (try to write a sample program and execute it, it will take more time than StringBuilder)
    

    StringBuilder:-

     StringBuilder is not synchronized 
     StringBuilder is not thread-safe
     StringBuilder performance is better than StringBuffer.
    

    Common thing :-

    Both have same methods with same signatures. Both are mutable.

    0 讨论(0)
提交回复
热议问题