What is this about Stringbuffer being synchronized ? What is the advantage of Stringbuilder?

Asked

Viewed 67 times

0

I know the difference between stringBuffer and stringbuilder is that one is synchronized and one is not, and that both represent strings that can be changed, but what synchronized means in practice?

  • Damn, I was almost done with the answer when it was closed.

  • 1

    @Bigown is not really a duplicate. This question is about synchronization, which the other one doesn’t talk about. The other question is about the extra space that is allocated in the StringBuffer/StringBuilder to improve performance. The question of synchronization is addressed only as an extra point in the utluiz answer - the remaining three answers say nothing about this.

  • But the answer is there, it’s not?

  • @bigown Well, I will not vote to reopen anyway. But if it were not the answer of utluiz, I would certainly do it.

  • Yeah, if I didn’t have his answer, I wouldn’t be answering. If a search had been made with both names that answer would have been found and the question would not have been posted.

  • 1

    @Victorstafusa if the answer adds something, put there even if it is only as a complement.

Show 1 more comment

1 answer

0

Stringbuilder vs Stringbuffer

Both are much faster for concatenating values than the common string and do exactly the same function. The main difference is that Stringbuffer is synchronized, while the other one is not. Thus, you ensure the consistency of your code when there are several threads reading or modifying the same String. For these cases, the ideal is to use Stringbuffer.

But Stringbuilder is still faster than Stringbuffer.

Source: http://www.devmedia.com.br/diferencas-entre-string-stringbuilder-e-stringbuffer-em-java/29865

  • 2

    Your answer tells what he has already put in the question.

  • Thus, you ensure the consistency of your code when there are several threads reading or modifying the same String. For these cases, the ideal is to use Stringbuffer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.