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 between String, StringBuffer and StringBuilder

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about the difference between String, StringBuffer and StringBuilder, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

String is a basic and important class in Java, and String is also a typical implementation of the Immutable class, which is declared as final class, and all other properties except hash are declared as final. Because of its immutability, for example, a lot of useless intermediate objects will be generated when concatenating strings, and frequent such operations will affect performance.

StringBuffer is to solve a large number of string splicing generated a lot of intermediate object problems and provide a class, provide append and add methods, you can add the string to the end of the existing sequence or specified location, its essence is a thread-safe modifiable character sequence, all the methods to modify the data are added with synchronized. But ensuring thread safety comes at a cost of performance.

In many cases, our string concatenation operation does not require thread safety, when StringBuilder comes on stage. StringBuilder is released by JDK1.5, which is essentially no different from StringBuffer, except that the part that ensures thread safety is removed and the overhead is reduced.

Both StringBuffer and StringBuilder inherit AbstractStringBuilder, and both take advantage of the modifiable char array (JDK 9 is followed by the byte array).

Therefore, if we have a large number of string concatenation, if we can predict the size, it is best to set capacity in new StringBuffer or StringBuilder to avoid the overhead of multiple capacity expansion. To expand the capacity, you need to discard the original array and copy the array to create a new array.

When we usually develop a small amount of string concatenation, we don't need to worry about it, for example

String str = "aa" + "bb" + "cc"

For strings without variables like this, the compilation phase will directly synthesize "aabbcc", and then see if there is any in the string constant pool (we will talk about the constant pool below). It will also be referenced directly. If not, it will be generated in the constant pool and the reference will be returned.

If it is with variables, in fact, the impact is not great, JVM will help us optimize it. (the following runs JDK version 1.8)

Look at the decompilation result. String splicing uses StringBuilder.append.

Do you seem to think that you don't need to use StringBuilder at ordinary times? just use String. It's all optimized for us.

No, look at the situation.

Looking at the decompilation result, new has StringBuilder many times.

That is to say, if there are a lot of strings concatenating new and many StringBuilder objects, so frequent string operations still have to use StringBuilder!

Let's talk about the string constant pool.

If you look at our code, you will find that String is really used frequently. Java introduces a string constant pool to avoid generating a large number of String objects in a system.

When creating a string, it first checks whether there is a string object with the same value in the pool, returns a reference directly, and does not create a string object; if not, creates a new string object and returns an object reference. and put the newly created object into the pool. However, the String object created by the new method does not check the string constant pool, but creates a new object directly in the heap and does not put the object into the pool. The above principles apply only to cases where references to String objects are assigned directly.

String str1 = new String ("a"); / / do not check the String str2 = "bb" of the string constant pool; / / check the

String also provides the intern () method. When this method is called, if a string equal to this String object (determined by the equals method) is included in the string constant pool, a reference to the string in the pool is returned. Otherwise, the String object is added to the pool and a reference to the object in the pool is returned.

In JDK6, it is not recommended to use a large number of intern methods, because this version of string cache is in the permanent generation, this space is limited and will not be clear except for FullGC, so a large number of caches are easy to OutOfMemoryError.

After reading the above, do you have any further understanding of the difference between String, StringBuffer and StringBuilder? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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