Basic features of String
1. Underlying structure
- For jdK1.8 and earlier, use the char[] array
- For JDK1.9 and later, byte[] is used for data storage
2. Immutability
- When a string is copied again, the specified memory area needs to be overridden and cannot be assigned with the original value
- When an existing string in the heap is concatenated, the assignment of the memory region needs to be reassigned. The original value cannot be used for assignment
- When you call String’s replace() method to modify a specified character or String, you need to assign a new memory area instead of using the original value
The string constant pool cannot store the same string within the same string
- The string constant pool size is 1009 in JDK1.6 and 60013 in JDK1.7 and later
- -xx :StringTableSize=100. You can change the size of the string constant pool by using this parameter
String memory allocation
1. History of change
For a history of the string constant pool, see the history of the method area.
Why this change in history? (1) The default of the permanent generation is smaller (2The garbage collection frequency of the permanent generation is lowCopy the code
The basic operation of String
String concatenation operation
Conclusion first:
- Concatenation of constants to constants results in the constant pool, which is the principle of compiler optimization
- No constant with the same content exists in the constant pool
- As long as one of them is a variable, the result is in the heap, and the principle for concatenation is StringBuilder
- If the concatenated result calls intern(), it actively puts string objects that are not already in the constant pool into the pool and returns the object address
Here’s an example:
@Test
public void test1(a){
String str1 = "a" + "b";
String str2 = "ab";
System.out.println(str1 == str2); //true
System.out.println(str1.equals(str2)); //true
}
@Test
public void test2(a){
String s1 = "java";
String s2 = "Python";
String s3 = "javaPython";
String s4 = s1 + "Python";
String s5 = s1 + s2;
String s6 = s4.intern();
System.out.println(s3 == s4); //false
System.out.println(s3 == s5); //false
System.out.println(s3 == s6); //true
}
/* s1+s2; /* s1+s2
@Test
public void test3(a){
String s1 = "a";
String s2 = "b";
String s3 = "ab";
String s4 = s1+s2;
System.out.println(s3 == s4); //false
}
Copy the code
V. Use of intern(
1. Let me start with two questions
Before we say intern(), let’s say two questions:
(1)new String("ab"How many objects will be created? The answer:2
(2)new String("a") +new String("b"How many objects will be created? The answer:6
Copy the code
First up:
object1:newKeyword object created in heap space2: ab in the string constant poolCopy the code
Having built on the first, let’s say the second:
object1: Because it is variable concatenation, it will firstnewThe StringBuilder () object2:new String("a"), heap objects3: object "A" in the constant pool4:new String("b"), heap objects5: object "B" in the constant pool6: created by the stringBuilder.toString () methodnew String("ab"), note that the string constant pool does not create "ab"Copy the code
2. Conclusion of using intern() method
The results of intern() are as follows:
- In jdk1.6, try to put this string object into the string constant pool
- If there is a string object in the string constant pool (permanent generation), it is not put in, and returns the address of the object in the existing string constant pool
- If there is no string object in the string constant pool, a copy of the object is put into the constant pool and the address of the object in the constant pool is returned
- Jdk1.7 and later, try to put this string object into the string constant pool
- If there is a string object in the string constant pool (heap), it will not be put in and will return the address of the object in the existing string constant pool
- If there is no string object in the string constant pool, a copy of the object’s reference address is put into the constant pool and the reference address in the constant pool is returned
3. Supplementary use examples
Code first:
String s1 = new String("a") + new String("b");
String s2 = s1.intern();
System.out.println(s1 == s2); / / jdk1.6 false jdk1.7 true
Copy the code
The above code yields different results in different JDK versions.