What is the difference between StringBuilder and StringBuffer? in Java

2

I think StringBuilder and StringBuffer in Java have the same functionality. What is the main difference?

    
asked by Giorgio Magaña 20.05.2017 в 23:00
source

2 answers

4

The biggest difference between the two classes is that StringBuffer has all its methods synchronized for "concurrent write support," while StringBuilder does not have them.

Given this, the performance of StringBuilder will be greater than StringBuffer in all its operations.

If you need to concatenate many strings / characters / numbers within a method that is invoked within a thread, it is best to use StringBuilder .

    
answered by 21.05.2017 в 00:04
0

about string builder I can tell you that it is similar to string, the difference is in:  when you create string objects, they are stored in the string pool (a special java site where you store the objects to be used). If a string that is already stored in the string pool is concatenated with another string ... 2 strings appear in the string pool, because the first string does not change ... it is simply copied with the modification.  StringBuilder is the opposite ... this type of strings if they are modified without being copied in the string pool ... so you modify a string it is not copied with the modification, but it is modified if only one is left in the stringpool. I recommend you read about the java heap and the java stack about StringBuffer I can not help you

    
answered by 21.05.2017 в 03:23