String, StringBuffer, StringBuilder, bufferbuilder
That is, String.
- String: unchangeable character sequence.
- StringBuffer: a variable string of thread-safe characters.
- StringBuilder: Non-thread security implementation of StringBuffer, JDK1.5 +.
Public final class String {private final char value []; public String (String original) {// cut the original String original into a character array and assign it to value [];} // StringBuffer public final class StringBuffer extends AbstractStringBuilder {char value []; // inherits the value [] public StringBuffer (String str) {super (str. length () + 16); // inherit the constructor of the parent class and create a size of str. length () + 16 value [] array append (str); // splits str into character sequences and adds them to value }}
1 constant pool
Each literal string in the Java source code is compiled into a class file to form a constant table with the flag number 8 (CONSTANT_String_info. When the JVM loads the class file, a memory data structure (StringTable) is created for the corresponding constant pool. The key is the hashcode of the string, value is the reference address of the string .), Stored in the method area. Meanwhile, JVM automatically creates a new String object (intern String object) in the heap for the String constant Literal Value in the CONSTANT_String_info constant table ). Then, convert the entry address of the CONSTANT_String_info constant table into the direct address of the String object in the heap (constant pool parsing ).
Example:
String SC = "AB" + "cd"; String sd = "abcd"; System. out. println (SC = sd); // true, which is loaded to the constant pool when the application is started.
2 StringBuilder and String +
String sa = "a";String s = sa + "b";
View vm commands in javap-c:
0: ldc #2 // String a 2: astore_1 3: new #3 // class java/lang/StringBuilder 6: dup 7: invokespecial #4 // Method java/lang/StringBuilder."<init>":()V 10: aload_1 11: invokevirtual #5 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 14: ldc #6 // String b 16: 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_2 23: return
From the above we can see that the actual operation of String + is new StringBuilder (). append (). append .... ToString (). Therefore, the following method does not cause efficiency problems (unless the link String is too long, because the default initialization size of String + operations is 16, which leads to multiple resize operations at append ):
String method(String sa) { return sa + "b";}
Generally, the low efficiency of String + is mainly caused by the following situations:
String method(String sa) { for (int i=0; i<100; i++) { sa += "b"; } return sa;}
Every time+GenerateStringBuilderObject, and thenAppend. Generate anotherStringBuilderObject. If we useStringBuilderFull object ProcessAppendYou can saveN-1The time when the object is created and destroyed.
Through the above description, we can know the following results:
String sa = "ab"; String sb = "cd"; String sab = sa+sb; String s = "abcd"; System.out.println(sab==s); // false
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 this table, this method returns the address of an existing String in the table, if there is no string with the same value in the table, register your own address in the table.
By using this method, we can test the JVM occupied by the constant pool.