The difference between StringBuffer and StringBuilder in Java

When I used java earlier, I knew that there was a class called StringBuffer, which was used to splice long strings. After turning to c#, there is also a class like function called StringBuilder, which is abbreviated as sb, which is very easy to remember.

Later, when I transferred back to Java, I found that Java also had StringBuilder, so I wondered why StringBuilder was launched after StringBuffer.

The original Java StringBuilder (like c#) is non thread safe, while the previous StringBuffer has certain thread safe properties. Of course, StringBuilder is introduced mainly because it is not necessary to use it in the case of multithreading.

The common use cases of StringBuilder (or StringBuffer) are:

In this case, StringBuilder is not a class member, it is just a local variable, and there is no multithreading problem at all.

Therefore, the introduction of StringBuilder has brought great performance improvement, and there is no security problem at all

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>