java - 为什么说String的拼接会产生很多的无用对象呢?
高洛峰
高洛峰 2017-04-18 10:49:32
0
3
807

为什么说String的拼接会产生很多的无用对象呢?
eg.
String str = "";
while(true){

str +="abc";

}

按我的理解,字符串拼接只会在常量池中创建共享对象,常量池的对象会被垃圾回收器回收吗?为什么说会产生很多无用的对象,需要垃圾回收,从而影响程序性能呢?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
迷茫

Regarding what the poster said, string splicing will only create shared objects in the constant pool, I don’t understand what it specifically means, but I can explain why this way of writing produces a lot of useless objects. I hope it can be helpful to the poster. helped.

Here are the answers:

Look at the picture above first. The String class in JDK is a final class, and the value used to store String characters is also final!

This means that the value of the string object cannot be modified after it is created! Therefore, the string generated by each loop must be a new String object. But the above code generates more objects than you think! pleasekeep looking at the pictures!

---------------------Beautiful dividing line---------------------- ------

The red box in the second picture is the decompiled code in the red box in the first picture. It can be seen from the picture:
a+=a; does the following things:

  1. Call String.valueOf(a). This step will not generate a new object because a is also a string.

  2. new StringBulid (the first object is actually the string a), this step will generate a Stringbuild object.

  3. .append (added string)

  4. .toString(), this step will call the toString method of Stringbuild.

Let’s take a look at Stringbuild’s toString() source code again

public String toString() {
        // Create a copy, don't share the array
        return new String(value, 0, count);
    }

Oops! Why is another String object created?

The analysis of a+=a is completed here, so every string addition operation in the loop actually creates at least one StringBuild object and one String object!

Peter_Zhu

String is an immutable object, so every time you change the String type, it is actually equivalent to generating a new String object, and then pointing the pointer to the new String object, so it is best not to use strings that frequently change their content. Use String, because every time an object is generated, it will have an impact on system performance. Especially when there are too many unreferenced objects in the memory, the JVM's GC will start to work, and the speed will definitely be quite slow.

For frequently modified variables, StringBuffer should be used~

小葫芦

... Only after jdk1.8 can StringBuild be used for multiple concatenations of strings. . The old version is not like this! ~~
I read an article last time 0 0, I don’t know if there is anything wrong.
Before 1.8, if you need to modify String multiple times, you need to use StringBuffer or StringBuilder.
1.8 has enabled syntactic sugar and modified this area, allowing us to enjoy high efficiency without explicitly calling StringBuilder.

And for string, you will only create a shared object in the constant pool,

str +="abc";
1.abc
2.abcabc
3........

Isn’t every object created here a useless object? In addition to the final results, GC recycling also takes time.

Because string is the final modification of char[], a new object must be created every time.
The reason why StringBuilder is so efficient is that the array is dynamically expanded, usually by adding operations, and only when the capacity is full Create a new larger char[], copy the old data and then add it. In summary, stringbuilder reduces the generation of objects and does not need to generate new objects every time. Please ask the experts for guidance! ~! ~! ~

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template