Since Java's Strings are stored in memory in a fixed-length manner, all additions, deletions, and modifications to Strings require the creation of new memory and memory copying.
The processing methods of StringBuffer and StringBuilder are different from ordinary String additions, deletions and modifications. They apply for memory first, thereby avoiding frequent memory applications.
For example, we splice "a", "b", and "c" respectively. If you use "a" + "b" + "c", (without optimization) Java will first apply for a two-byte memory and copy "a" and "b" to this memory. Then apply for a three-byte memory and copy "ab" and "c" to this memory. With StringBuffer or StringBuilder, they will first apply for a large memory (default is 16 bytes). When we splice, we only need to directly copy "a", "b", and "c" to this memory. , no need to repeatedly apply for memory. The form of applying first and then using it here reflects the concept of buffer zone.
No new objects are generated. Each time + is used with String, a new object will be generated. Of course, the compiler will perform compilation optimization in a short period of time, and the buffer will be directly appended to the original object and will not generate a new object
Take StringBuilder as an example here. Look at the source code. The internal storage of StringBuilder is stored using char array. When appending, the following string will be copied to the char array inside StringBuilder. If the internal buffer is insufficient, System will be used. .arrayCopy is used to implement an expansion mechanism, which is very efficient. This is how buffering is implemented.
String d = "a" + "b" + "c"; will produce multiple string objects. As answered above, the reason is that there is a string buffer pool inside the virtual machine.
Since Java's Strings are stored in memory in a fixed-length manner, all additions, deletions, and modifications to Strings require the creation of new memory and memory copying.
The processing methods of StringBuffer and StringBuilder are different from ordinary String additions, deletions and modifications. They apply for memory first, thereby avoiding frequent memory applications.
For example, we splice "a", "b", and "c" respectively. If you use "a" + "b" + "c", (without optimization) Java will first apply for a two-byte memory and copy "a" and "b" to this memory. Then apply for a three-byte memory and copy "ab" and "c" to this memory.
With StringBuffer or StringBuilder, they will first apply for a large memory (default is 16 bytes). When we splice, we only need to directly copy "a", "b", and "c" to this memory. , no need to repeatedly apply for memory. The form of applying first and then using it here reflects the concept of buffer zone.
No new objects are generated. Each time + is used with String, a new object will be generated. Of course, the compiler will perform compilation optimization in a short period of time, and the buffer will be directly appended to the original object and will not generate a new object
Take StringBuilder as an example here. Look at the source code. The internal storage of StringBuilder is stored using char array. When appending, the following string will be copied to the char array inside StringBuilder. If the internal buffer is insufficient, System will be used. .arrayCopy is used to implement an expansion mechanism, which is very efficient. This is how buffering is implemented.
String d = "a" + "b" + "c"; will produce multiple string objects. As answered above, the reason is that there is a string buffer pool inside the virtual machine.
Normal "a" + "b" + "c"
It will generate "ab" string object
But using those two classes will only generate the last "abc" object