Also say string.
- String: The immutable sequence of characters.
- StringBuffer: A variable sequence of characters for thread safety.
- Stringbuilder:stringbuffer non-thread-safe implementation, jdk1.5+.
Public Final classString {Private Final Charvalue[]; Publicstring (string original) {//original The original string into a character array and assigns it to value[]; } }//StringBuffer Public Final classStringBufferextendsAbstractstringbuilder {CharValue[];//inherits the value[in the parent class Abstractstringbuilder] PublicStringBuffer (String str) {Super(Str.length () + 16);//inherits the constructor of the parent class and creates a value[] array with a size of str.length () +16append (str);//cut str into a sequence of characters and add to value[] } }
1 constant Pool
Each literal string in the Java source code is compiled into a class file stage, forming a constant table with a flag number of 8 (constant_string_info). When the JVM loads the class file, it creates a memory data structure for the corresponding constant pool (stringtable, which is a hashtable,key that is the string hashcode,value is the reference address of the string. ), and stored in the method area. At the same time, the JVM automatically creates a new string object (Intern string object) in the heap for string constant literals in the Constant_string_info constant table. The entry address of the Constant_string_info constant table is then transformed into the direct address (constant pool resolution) of the String object in the heap.
Example:
String sc= "AB" + "CD"; String SD= "ABCD"; System.out.println (SC//
2 StringBuilder and string+
String sa == sa + "B";
Javap-c View virtual machine directives:
0:LDC #2//String a2: Astore_13:New#3//class Java/lang/stringbuilder6: DUP7:invokespecial #4//Method Java/lang/stringbuilder. " <init> ":() V10: Aload_111:invokevirtual #5//Method java/lang/stringbuilder.append: (ljava/lang/string;) Ljava/lang/stringbuilder;14:LDC #6//String b16:invokevirtual #5//Method java/lang/stringbuilder.append: (ljava/lang/string;) Ljava/lang/stringbuilder;19:invokevirtual #7//Method java/lang/stringbuilder.tostring: () ljava/lang/string;22: astore_223:return
As can be seen from the string+, the actual operation is new StringBuilder (). Append (). append....tostring (). Therefore, the following methods do not cause efficiency problems (unless the link string is longer, because the default initialization size of the string+ operation is 16 causes append multiple resize):
string Method (String sa) { return sa + "b";}
Generally speaking, the inefficiency of string+ is mainly produced in the following cases:
string Method (String sa) { for (int i=0; i<100; i++) { + = "B"; } return sa;}
every time. + will produce a StringBuilder object, and then Append And then throw it away. The next loop re-produces a StringBuilder object. If we use the StringBuilder object directly append , we can save N-1 times when objects are created and destroyed.
Using the instructions above, we can know the following results:
String sa = "AB"; = "CD"; = sa+= "ABCD"; System.out.println (SAB//
3 String.intern
String.intern can save a string class to a global stringtable table, if a Unicode string with the same value is already in the table, the method returns the address of the existing string in the table, and if there is no string of the same value in the table, register its address in the table.
In this way we can test which area of the JVM the constant pool occupies.
String, StringBuffer, StringBuilder