Click to enter _ more _java thousand ask
1. What is a string
The Java.lang.String class represents a string that is usually bright, so-called string, which is a set of characters composed of character sets. It has the following characteristics:
String is an immutable object
Each time a string is changed, it is actually equivalent to generating a new string object, and then pointing the pointer to the new string object (if you do not use the new construct, you are actually looking from the string pool in the heap to see if the string has been saved, and if so, point directly to the If not, put the string into the string pool and point to it first.
Therefore, it is best not to use string to change the content of the strings, each generation of the object will have an impact on system performance (especially when there is no reference object in the heap, the JVM garbage collection GC will start to work, performance will be affected).
Learn about garbage collection here: [What is the Java garbage collection mechanism][2]
Learn how string is stored in memory see here: How string is stored in memory
Comparison between string and stringbuffer efficiency
In some special cases, the string object will not change at a slower rate than the StringBuffer object, especially in the following string object generation, where string efficiency is far faster than StringBuffer:
String"hello ""world"new"hello ""world" );
You'll be surprised to find that the speed at which the string object was generated was simply too fast, and at this time StringBuffer had no advantage at all.
In fact, this is a JVM trick, in the JVM's eyes, this
String"hello ""world" ;
is equivalent to:
String"hello world" ;
So it doesn't take much time. Note, however, that if your string is from another string object, the JVM will behave in a way that creates several objects, such as:
String"hello " ;String"world" ;String"hello world" ;
String also has two similar common classes, stringbuffer/stringbuilder, to understand their use and differences look here: Stringbuffer/stringbuilder What's the Difference
Java thousand ask _06 data Structure (020) _string is what