Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference among String,StringBuilder,StringBuffer in Java?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about what is the difference among String,StringBuilder,StringBuffer in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The difference between these three classes is mainly in two aspects, namely, running speed and thread safety.

First of all, the running speed, or the execution speed, is StringBuilder > StringBuffer > String.

The reason why String is the slowest:

String is a string constant, while StringBuilder and StringBuffer 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. Take the following code as an example:

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 StringBuilder and StringBuffer 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.

In addition, sometimes we assign strings like this

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 String is much faster than StringBuilder because of the actions in line 1 and

String str= "abcde"

Is exactly the same, so it will be very fast, and if it is 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.

two。 Let's talk about thread safety.

In terms of thread safety, StringBuilder is not thread safe, while StringBuffer is thread safe.

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.

3. To sum up.

String: suitable for situations with a small number of string operations

StringBuilder: suitable for situations where a large number of operations are performed in a character buffer in a single thread

StringBuffer: suitable for multithreading to perform a large number of operations in the character buffer

Thank you for reading! This is the end of the article on "what is the difference between String,StringBuilder,StringBuffer in Java". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report