String StringBuffer Stringbuilder usage and difference

String is a string constant

StringBuffer is string variable (thread safe)

StringBuilder is string variable (thread unsafe)

The biggest difference between StringBuffer, StringBuilder and String is that String is not constant, so every time you operate on a String object, a new String object will be generated, and then the pointer will point to the new String object, so you need to change the content of the string frequently It is best not to use String, because every time an object is generated, it will have an impact on system performance. After there are too many unreferenced objects in memory, the speed will definitely be affected.

The difference between StringBuffer and StringBuilder is that StringBuffer adds a synchronized modifier before the append method, which plays a role in synchronization, but it also reduces the execution efficiency; if you want to use a loop in the toString method, use StringBuilder.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325924398&siteId=291194637