In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "java string splicing in when to go StringBuilder", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "java string splicing in when to go StringBuilder" article.
one。 Problem case 1
Test code 1.png
You can see that the result of str = = str2 is false, so let's look at the next example.
Case 2
Test code 2.png
At this point, the result of the comparison of the two strings is true.
two。 Explore the problem
At this time, the question arises. Why are the results inconsistent? * decompiled the bytecode file by entering the javap-c TestDemo.class command in the cmd window, and found the problem? *
Test code 1cmd.png
As you can see in case 1, the bottom layer of the java code goes to StringBuilder, performs string concatenation, and then calls the toString method of StringBuilder.
Test code 2cmd.png
In case 2, the class file was decompiled and found that the code had changed a little and did not use StringBuilder for string concatenation.
1. In case 1, through variable and string concatenation, java needs to find the corresponding value of the variable in memory before completing the string stitching work. In this way, the java compiler can not be optimized, so it can only go to StringBuilder to assemble the string, and then call the toString method. Of course, the returned result is different from the memory address of the 111string in the constant pool, so the result is false.
2, in case 2, write the value directly in the expression, java does not have to look for the corresponding value in memory according to the variable, but can optimize the expression directly when compiling, the optimized expression changes directly from "111" +" to "111", the two String type variables all point to the 111string of the constant pool, so the result is true.
The above is about "java string splicing when to go StringBuilder" this article content, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge content, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.