The difference between StringBuilder and String

Same point:

Both StringBuilder and String are used to receive and process string type data

difference:

        When String concatenates strings, each string is first created in memory, and then the strings are concatenated into a new string and returned. This will not be efficient and perceptual for several strings difference.

StringBuilder is a dynamic object, and the string it represents is variable. Its string splicing principle is to add the object to be spliced ​​after the address of the object, without repeatedly opening up and creating new addresses, saving It saves memory resources and improves splicing performance. When there are a large number of splicing requirements or unknown splicing times, it is recommended to use StringBuilder to splice strings.

Notice:

        When modifying string information, it is not allowed to create an object at this time, you can use the stringBuilder object

        After splicing using stringBuilder object, the type of object is StringBuilder type, need toString to convert to String type.

        Personally, if the number of splicing exceeds 5 times, you can consider using StringBuilder for splicing operations.

Guess you like

Origin blog.csdn.net/guliudeng/article/details/125774555