In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
The main content of this article is to explain "what is the difference between String,StringBuffer,StringBuilder". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the difference between String,StringBuffer,StringBuilder"?
operational speed
From fast to slow:
StringBuilder > StringBuffer > String
The reason for the slowest String
String is a string constant, while StringBuffer and StringBuilder are string variables, that is, once a String object is created, the object is immutable, but the objects of the latter two are variables and can be changed.
Example: 1
1 String str= "abc"
2 System.out.println (str)
3 str=str+ "de"
4 System.out.println (str)
If you run this code, you will find that first output "abc", and then output "abcde", it seems that the object str has been changed, in fact, this is just an illusion, JVM for these lines of code is handled like this, first create a String object str, and assign "abc" to str, and then in the third line, in fact, JVM creates a new object also called str Then add the value of the original str and "de" and assign it to the new str, and the original str will be recycled by JVM's garbage collection mechanism (GC), so the str has not actually been changed, that is, once the String object is created, it cannot be changed. 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.
On the other hand, the objects of StringBuffer and StringBuilder are variables, and the operation on the variable is to change the object directly without creating and recycling, so it is much faster than String.
Example: 2
1 String str= "abc" + "de"
2 StringBuilder stringBuilder=new StringBuilder () .append ("abc") .append ("de")
3 System.out.println (str)
4 System.out.println (stringBuilder.toString ())
The output is also "abcde" and "abcde", but the reaction speed of String is much faster than that of StringBuilder, because the operation in line 1 is exactly the same as String str= "abcde", so it is very fast, and if written in the following form:
1 String str1= "abc"
2 String str2= "de"
3 String str=str1+str2
Then JVM will continue to create and recycle objects to do this, as mentioned above. The speed will be very slow.
Thread safety
StringBuffer is thread safe
StringBuilder is thread-unsafe
If a StringBuffer object is used by multiple threads in a string buffer, many methods in StringBuffer can have the keyword synchronized, so you can guarantee that the thread is safe, but the StringBuilder method does not have this keyword, so thread safety can not be guaranteed, and some wrong operations may occur. So if the operation to be done is multithreaded, then use StringBuffer, but in the case of a single thread, it is recommended to use the faster StringBuilder.
At this point, I believe you have a deeper understanding of "what is the difference between String,StringBuffer,StringBuilder". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.