Is the "+" of the string in Java obfuscation worse than the append of stringbuilder?

Source: Internet
Author: User

1) code:

public static String s1() {String result = "";result += "A";result += "B";result += "C";return result;}public static String s2() {String result = "";result = "A" + "B" + "C";return result;}public static String s3() {StringBuilder result = new StringBuilder();result.append("A").append("B").append("C");return result.toString();}

2) Conclusion:Performance: S2> S3> S1

3) Analysis:

View the bytecode in javap:


We can see that:

S1 method:Three times to create a stringbuilder object, six times to call the append method to add a string, and three times to call the tostring method to return the added result to the result. Among them, the performance is the worst.

S2 method:The compiler optimizes "+", merges "A" + "B" + "C" into "ABC", and directly assigns the result. Among the three, the best performance is possible.

S3 method:The stringbuilder object is created once, the append method is called three times to add a string, and the tostring method is called once to return the added result to the result. Among the three, the performance is centered.

4) Conclusion:

  • The append method is not necessarily the best for string connection operations.
  • Right of equal signVariable participationThe string "+" operation (for example: Result + = "A"). When JVM creates a connection, it creates a new stringbuilder object and then uses its append method to implement the connection, right of equal signNo variable involved(For example, result = "A" + "B" + "C"). When JVM performs a connection, it will optimize the connection operation on the right, merge it into a string and assign it to the variable on the left. Then it becomes a simple value assignment process.

5) Supplement

5.1) code:

public static String s4() {String result = "";result = result + "A" + "D";return result;}public static String s5() {String result = "";result += "A" + "D";return result;}

5.2) Analysis:

View the bytecode in javap:

 

We can see that:

S4 method:Create a stringbuilder object once and call the append method three times to add a string(A and D each occupy one time), The tostring method is called once to return the added result to the result.

S5 method:Create a stringbuilder object once and call the append method twice to add a string(A and D are optimized to AD, so A and D count only once), The tostring method is called once to return the added result to the result.

5.3) Conclusion:

  • The variables on the Right of S4 and S5 are involved in the "+" Operation (result) of the string. Therefore, the stringbuilder object is created and connected using its append method.
  • S4 uses "=", so the result variable is explicitly indicated on the right of the equal sign to participate in the "+" Operation of the string. In this case, A and D are completed by append twice, while S5 uses "+ =". Therefore, the result variable is implicitly indicated on the right of the equal sign to participate in the "+" Operation of the string, in this case, A and D are merged into ad and completed at one append.

6) Expansion

Why do you think stringbuilder (or stringbuffer) occupies less memory?

I think like this:

(1) What do we ultimately want?
We finally want to be a string. In a more accurate language, we finally want to open up a memory on the stack and put the desired string in the memory, then, return the starting address of the memory and put it in our variable so that we can hold a reference to the memory.

(2) What is the role of stringbuilder (or stringbuffer?
Stringbuilder is used to help us complete the above operations.

(3) Why should we use stringbuilder?
Because the string object is immutable. to modify the existing String object, we must use the stringbuilder intermediate object, whether we create a stringbuilder or JVM for us to create it.

(4) How can we get the desired string through stringbuilder?
Call the tostring method of stringbuilder to return our string. Specifically, it should be a reference to the memory on the stack maintained by stringbuilder, in this memory, stringbuilder has put the string we want in it.

(5) What will stringbuilder do after calling the stringbuilder tostring?
Stringbuilder will die because its lifecycle has ended.

(6) What do we finally get?
What we finally get is what we want, that is, the memory allocated on the stack to store the string we finally want, and the starting address of this memory has been placed in our variable, we already hold a reference to this memory.

Through the above analysis, we can see that stringbuilder is only an intermediate state, and its function is only to help us complete the operations we want and achieve the purpose, stringbuilder will die out, however, the emergence of this intermediate state will inevitably occupy the memory (for example, the loading of its class and the creation of its instance object ), if we can avoid this intermediate state (the situation described in the S2 method above), the performance will naturally improve, although I have never tried it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.