String-related knowledge summary
Immutability of strings
Outline
String is declared final, so it can not be inherited
In Java8, String char internal data storage array
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
}
After Java9, to achieve the String class instead byte array to store a string, use coder
to indicate which encoding used.
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final byte[] value;
/** The identifier of the encoding used to encode the bytes in {@code value}. */
private final byte coder;
}
value array is declared as final, can not refer to other arrays which means that after the array initialization value. String is not changed and the internal value of the method, which can ensure String immutable.
Connection string
String s = "abcd";
s.concat("ef");
s
The reference is stored in a re-created out of the string object.
If you need to modify a string, you should use StringBuffer or StringBuilder. Otherwise there will be a lot of time wasted on garbage collection, because every time there is a new attempt to modify string object is created.
Immutable benefits
1. The hash value may be cached
Because the hash value String is often used, for example, as the HashMap String key. Immutable characteristics may be such that the hash value is not variable, so only calculated once.
2. String Pool in need
If a String object is created and passed, it will obtain references from the String Pool. Only String is immutable, it is possible to use String Pool.
3. Security
String is often used as parameter, String parameters can be guaranteed immutability immutable. For example, if String is variable, then the network connection process, String is changed in the case of a network connection parameters, change the String object that the party that is now connected to other hosts, but the reality is not certain.
4. Security thread
String immutability born with thread-safe, can be used in multiple threads.
String,StringBuffer,StringBuilder
1. Variability
- String immutable
- StringBuffer and StringBuilder variable
2. Thread Safety
- String immutable and therefore thread-safe
- StringBuilder is not thread safe
- StringBuffer is thread-safe, internally synchronized to synchronize
String Pool
String constant pool (String Pool) holds all literal strings (literal strings), the literal will be determined at compile time. Moreover, also possible to use a String intern () method will be added to the string during running in String Pool.
When a string called a intern () method, if there is already a String Pool string and the string values are equal (using equals () method for determination), it will return a reference string String Pool; otherwise, We will add a new string in the string Pool and returns a reference to the string.
The following example, s1 and s2 using the new String () a new way of two different strings, while s3 and s4 are () method to obtain a reference string by s1.intern. intern () first reference string into String Pool s1, and then returns the string reference. Thus s3 and s4 refer to the same string.
String s1 = new String("aaa");
String s2 = new String("aaa");
System.out.println(s1 == s2); // false
String s3 = s1.intern();
String s4 = s1.intern();
System.out.println(s3 == s4); // true
If you are creating a string in the form of "bbb" of this literal, the string will be automatically placed in the String Pool.
String s5 = "bbb";
String s6 = "bbb";
System.out.println(s5 == s6); // true
Before Java 7, String Pool are placed runtime constant pool, it is a permanent generations. In Java 7, String Pool is moved to the heap. This is because of the limited space on behalf of a permanent, large-scale use in a string of scenarios will lead to an OutOfMemoryError.
new String("abc")
Using this method creates two string objects (provided that the String Pool) has not "abc" string object.
- "Abc" belongs to the string literal, the compiler period will create a string object String Pool, point to the "abc" string literal;
- The use of the new approach will create a string object in the heap.
Create a test class, which is the main method used in this way to create a string object.
public class NewStringTest {
public static void main(String[] args) {
String s = new String("abc");
}
}
Use javap -verbose decompile, get the following:
// ...
Constant pool:
// ...
#2 = Class #18 // java/lang/String
#3 = String #19 // abc
// ...
#18 = Utf8 java/lang/String
#19 = Utf8 abc
// ...
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=3, locals=2, args_size=1
0: new #2 // class java/lang/String
3: dup
4: ldc #3 // String abc
6: invokespecial #4 // Method java/lang/String."<init>":(Ljava/lang/String;)V
9: astore_1
// ...
In the Constant Pool, the # 19 stores this string literal "abc", # 3 is the String Pool string object, it points to # 19 this string literal. In the main method, 0: OK heap using new # 2 creates a String object, using the ldc # 3 Pool String String object as a parameter to the constructor of the String.
The following is the source String constructor, you can see that in the constructor as a parameter another String object, and will not fully replicate the contents of a string array of value objects, but will point to the same value array.
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
reference
- https://github.com/CyC2018/CS-Notes/blob/master/notes/Java%20%E5%9F%BA%E7%A1%80.md#%E4%BA%8Cstring
- https://github.com/hollischuang/toBeTopJavaer/blob/master/basics/java-basic/final-string.md
If infringement, please contact deleted!