Difference between StringBuilder and StringBuffer

后端 未结 30 2231
独厮守ぢ
独厮守ぢ 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:37

    The major difference is StringBuffer is syncronized but StringBuilder is not.If you need to use more than one thread , then StringBuffer is recommended.But, as per the execution speed StringBuilder is faster than StringBuffer , because its not syncronized .

    0 讨论(0)
  • 2020-11-21 15:37
    • StringBuffer is thread-safe but StringBuilder is not thread safe.
    • StringBuilder is faster than StringBuffer.
    • StringBuffer is synchronized whereas StringBuilder is not synchronized.
    0 讨论(0)
  • 2020-11-21 15:38

    StringBuffer

    StringBuffer is mutable means one can change the value of the object . The object created through StringBuffer is stored in the heap . StringBuffer has the same methods as the StringBuilder , but each method in StringBuffer is synchronized that is StringBuffer is thread safe .

    because of this it does not allow two threads to simultaneously access the same method . Each method can be accessed by one thread at a time .

    But being thread safe has disadvantages too as the performance of the StringBuffer hits due to thread safe property . Thus StringBuilder is faster than the StringBuffer when calling the same methods of each class.

    StringBuffer value can be changed , it means it can be assigned to the new value . Nowadays its a most common interview question ,the differences between the above classes . String Buffer can be converted to the string by using toString() method.

    StringBuffer demo1 = new StringBuffer(“Hello”) ;
    // The above object stored in heap and its value can be changed .
    
    demo1=new StringBuffer(“Bye”);
    // Above statement is right as it modifies the value which is allowed in the StringBuffer
    

    StringBuilder

    StringBuilder is same as the StringBuffer , that is it stores the object in heap and it can also be modified . The main difference between the StringBuffer and StringBuilder is that StringBuilder is also not thread safe. StringBuilder is fast as it is not thread safe .

    StringBuilder demo2= new StringBuilder(“Hello”);
    // The above object too is stored in the heap and its value can be modified
    
    demo2=new StringBuilder(“Bye”);
    // Above statement is right as it modifies the value which is allowed in the StringBuilder
    

    Resource: String Vs StringBuffer Vs StringBuilder

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

    String is an immutable.

    StringBuffer is a mutable and synchronized.

    StringBuilder is also mutable but its not synchronized.

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

    Here is the performance testing result for String vs StringBuffer vs StringBuilder. Finally, StringBuilder won the Test. See below for test code and result.

    Code:

    private static void performanceTestStringVsStringbuffereVsStringBuilder() {
    // String vs StringBiffer vs StringBuilder performance Test
    
    int loop = 100000;
    long start = 0;
    
    // String
    String str = null;
    start = System.currentTimeMillis();
    for (int i = 1; i <= loop; i++) {
      str += i + "test";
    }
    System.out.println("String - " + (System.currentTimeMillis() - start) + " ms");
    
    // String buffer
    StringBuffer sbuffer = new StringBuffer();
    start = System.currentTimeMillis();
    for (int i = 1; i <= loop; i++) {
      sbuffer.append(i).append("test");
    }
    System.out.println("String Buffer - " + (System.currentTimeMillis() - start) + " ms");
    
    // String builder
    start = System.currentTimeMillis();
    StringBuilder sbuilder = new StringBuilder();
    for (int i = 1; i <= loop; i++) {
      sbuffer.append(i).append("test");
    }
    System.out.println("String Builder - " + (System.currentTimeMillis() - start) + " ms");
    
      }
    

    Execute Me on ideone

    Result:

    100000 iteration for adding a single text

    String - 37489 ms
    String Buffer - 5 ms
    String Builder - 4 ms
    

    10000 iteration for adding a single text

    String - 389 ms
    String Buffer - 1 ms
    String Builder - 1 ms
    
    0 讨论(0)
  • 2020-11-21 15:41

    StringBuilder and StringBuffer are almost the same. The difference is that StringBuffer is synchronized and StringBuilder is not. Although, StringBuilder is faster than StringBuffer, the difference in performance is very little. StringBuilder is a SUN's replacement of StringBuffer. It just avoids synchronization from all the public methods. Rather than that, their functionality is the same.

    Example of good usage:

    If your text is going to change and is used by multiple threads, then it is better to use StringBuffer. If your text is going to change but is used by a single thread, then use StringBuilder.

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