Explain the performance difference between string and StringBuilder in Java in terms of memory

In the past, I often saw discussions on Java string splicing on the Internet. I see that some Java developers write in their suggestions to novice programmers as follows:

Do not use the + sign to splice strings. Use the append() method of StringBuffer or StringBuilder to splice strings. However, using the + sign to splice strings is really so annoying. Isn't there any merit in using the + sign to splice strings?

By consulting the Java API documentation about the string class, We can see the following fragment: "the Java language provides special support for string concatenation (" + ") and converting other objects into strings. String concatenation is through StringBuilder (or StringBuffer) class and its append method. String conversion is implemented through the toString method, which is defined by the object class and can be inherited by all classes in Java. "

This passage clearly tells us that using the + sign to splice strings in Java is actually implemented by StringBuffer or StringBuilder and its append method.

In addition to the Java API documentation, we can also use the tool to view the bytecode command of the class file to get the above answer. Example code:

The corresponding bytecode commands can be viewed through the tool as follows:

From the bytecode command, we can clearly see the following code we write

It is converted into the following statements by the compiler:

Not only that, the java compiler is also a smart compiler. When the + sign is spliced with all string literals, the java compiler will intelligently convert it into a complete string at compile time. For example:

The java compiler directly splices this all literal string and converts it into a complete string at compile time.

Even if there are variables in the string spliced with the + sign, the java compiler will merge the first string literal into one string.

It can be seen from the above that there is no problem using the + sign to splice multiple strings at one time, such as string STR = STR1 + STR2 + str3 + str4.

In Java, string objects are immutable. In the code, you can create multiple aliases for a string object. But these aliases are the same, and the references are the same. For example, both S1 and S2 are "droidyue Alias of "com" object, which holds references to real objects. So S1 = S2

And in Java, the only overloaded operator is string splicing related. +, + =. In addition, Java designers do not allow overloading of other operators.

In Java, the only overloaded operator is string splicing related. +, + =. In addition, Java designers do not allow overloading of other operators.

As we all know, before java version 1.4, string splicing can use StringBuffer. Starting from Java 1.5, we can use StringBuilder to splice strings. The main difference between StringBuffer and StringBuilder is that StringBuffer is thread safe and suitable for multithreading string operation; StringBuilder is thread unsafe and is suitable for operating strings in a single thread. However, most of our string splicing operations are performed in a single thread, so using StringBuilder can improve performance.

Before Java 1.4, the compiler used StringBuffer to process strings spliced with + signs; Since Java 1.5, the compiler has mostly used StringBuilder to process strings spliced with + signs.

When we write code in the JDK 1.4 environment, it is recommended to use the + sign for the above case of splicing multiple strings at one time. In this way, when JDK is upgraded to version 1.5 or above, the compiler will automatically convert it into StringBuilder to splice strings, so as to improve the efficiency of string splicing.

Of course, it is recommended to use the + sign to splice strings only when splicing multiple strings in one statement. If you splice a string in multiple statements, it is still recommended to use StringBuffer or StringBuilder. For example:

The byte commands compiled by the compiler are as follows:

From the above picture, we can see that each + splicing statement creates a new StringBuilder object. This situation is particularly obvious under cyclic conditions, resulting in relatively large performance loss. Therefore, it is strongly recommended to use StringBuffer or StringBuilder to splice strings in multiple statements.

About the optimization brought by using StringBuilder

In addition, when using StringBuffer or StringBuilder, we can also use the following methods to further improve performance (the following code takes StringBuilder as an example, and StringBuffer is similar).

1. Predict the maximum length of the final string.

The default length of StringBuilder's internal char array is 16. When we append a string and exceed this length, StringBuilder will expand the internal array capacity to meet the needs. In this process, StringBuilder will create a new large capacity char array and copy the data in the original array to the new array. If we can roughly predict the maximum length of the string finally spliced, we can specify the appropriate initial capacity when creating the StringBuilder object. For example, we need to splice a string containing 100 letters a. You can write the following code:

Balance according to the actual situation to create a StringBuilder suitable for the initial capacity.

2. For a single character, try to use char type instead of string type.

Sometimes, we need to append a single character after the string (for example: a). At this time, we should use it as much as possible

Instead of using:

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
分享
二维码
< <上一篇
下一篇>>