In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the difference between StringBuilder and StringBuffer in Java? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
01
StringBuilder and StringBuffer have basically the same member properties and member methods, except that the member method of StringBuffer is preceded by the synchronized keyword to ensure thread safety, so the speed of StringBuffer is slower than that of StringBuilder.
Map Town Building:
02
The last reason for String is that it is a string constant, while StringBuilder and StringBuffer are string variables, that is, once String is created, the object is immutable, but the other two are variables and can be changed.
Look at the code:
The structure of the output of the two methods is the same, so let's see if the address of the variable str is the same on lines 25 and 27. The code runs as follows:
We can see that the address is 497in line 25 and 499 in line 27, which means that Java first creates a String object str and assigns "abc" to str, and then in line 27, JVM creates a new object also called str, and then adds up the value of the original str and "de" to the new str, and the original str will be recycled by JVM's garbage collection mechanism (GC). The str has not actually been changed, that is, the String object mentioned earlier cannot be changed once it is created. Therefore, the operation on String objects in Java is actually a process of constantly creating new objects and recycling old ones, so the execution speed is very slow.
Let's see if the variable stringBuffer has changed in lines 30 and 32. The code runs as follows:
After reading the above, have you mastered the difference between StringBuilder and StringBuffer in Java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.