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!
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:
Call String.valueOf(a). This step will not generate a new object because a is also a string.
new StringBulid (the first object is actually the string a), this step will generate a Stringbuild object.
.append (added string)
.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!
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! ~! ~! ~
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:
Call String.valueOf(a). This step will not generate a new object because a is also a string.
new StringBulid (the first object is actually the string a), this step will generate a Stringbuild object.
.append (added string)
.toString(), this step will call the toString method of Stringbuild.
Let’s take a look at Stringbuild’s toString() source code again
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!
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,
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! ~! ~! ~