Java string buffer pool Analysis
Java virtual machines open a separate area in the memory to store string objects. This memory area is called the string buffer pool. How does the java string buffer pool work?
String a = abc;String b = abc;String c = new String(xyz);
For example, the above Code:
String a = "abc ";
When creating a string, check whether the same object exists in the string buffer pool. If the same object exists, the system directly returns the reference of this object, if there are no identical objects, create the object in the string buffer pool and return the application of the object. For this step, the buffer pool does not contain the string object abc. Therefore, first create a String object and return the object reference to.
String B = "abc ";
This statement also creates an object to reference variable B and point it to the abc object. At this time, first look for the string buffer pool and find that the object abc already exists. This directly returns the reference of this object to B. At this time, a and B share an object abc, but don't worry, a changes the string and affects B, because the strings are constants. Once created, a cannot be modified unless a new object is created.
String c = new String ("xyz"); (for the implementation of this constructor, see the appendix)
The query string buffer pool finds that the string object xyz does not exist. Therefore, a zyx object is created in the string buffer pool and then the reference is returned.
From the above analysis, we can see that when a new string is created, it is not necessarily a new object. It may be that the same object is used together with other referenced variables. The following describes several common issues related to the string buffer pool.
Several string objects have been created.
String a = abc; String b = abc; String c = new String(xyz); String d = new String(xyz); String e=ab+cd;
This program is similar to the above program. Let's look at the comparison:
String a = "abc"; this sentence creates an object because the buffer pool does not contain the String object abc; String B = "abc "; because the object abc already exists in the buffer pool, no new object will be created; String c = new String ("xyz"); because no String object xyz exists, therefore, a xyz object is created first, and then the String object is constructed as a String, and a new String object is created in memory (not in the buffer pool, therefore, two objects are created; String d = new String ("xyz"); the process of creating an object is omitted, so only one object is created; string e = "AB" + "cd"; the constant value is determined during compilation. Therefore, this sentence is equivalent to String e = "abcd"; an object is created.
Therefore, the number of created objects is: 1, 0, 2, 1, 1
Equal or not
When learning java, we know that the two string objects must be equal to equal and cannot use =. However, after learning the string buffer pool, we should know why = cannot be used, under what conditions = is equivalent to equal. First, you must know that = is used to compare the memory addresses of the two objects. Let's look at the following programs:
public static void main(String[] args) { String s1 = Monday; String s2 = Monday; if (s1 == s2) System.out.println(s1 == s2); else System.out.println(s1 != s2); }
Output result: s1 = s2
Analysis: through the introduction of the string buffer pool above, we know that both s1 and s2 point to the same object in the string buffer pool, so the memory address is the same, therefore, you can use = to determine whether two strings are equal.
public static void main(String[] args) { String s1 = Monday; String s2 = new String(Monday); if (s1 == s2) System.out.println(s1 == s2); else System.out.println(s1 != s2); if (s1.equals(s2)) System.out.println(s1 equals s2); else System.out.println(s1 not equals s2); }
Output result: s1! = S2
S1 equals s2
Analysis: from the above analysis, we know that String s2 = new String ("Monday"); this sentence does not create a new object in the String buffer pool, however, a new object is created at other locations in the memory, so s1 points to the string buffer pool, s2 points to other locations in the memory, and the memory addresses of the two are different.
public static void main(String[] args) { String s1 = Monday; String s2 = new String(Monday); s2 = s2.intern(); if (s1 == s2) System.out.println(s1 == s2); else System.out.println(s1 != s2); if (s1.equals(s2)) System.out.println(s1 equals s2); else System.out.println(s1 not equals s2); }
Output result: s1 = s2
S1 equals s2
Analysis: Let's talk about the role of the intern () method. The function of this method is to return the reference of the object in the string buffer pool, so s2 also points to the address in the string buffer pool, and s1 are equal.
public static void main(String[] args) { String Monday = Monday; String Mon = Mon; String day = day; System.out.println(Monday == Mon + day); System.out.println(Monday == Mon + day); }
Output result: true
False
Analysis: Why is the first variable equal to true? We have already said this, because both are constants, so we can determine at the compilation stage. In the second one, day is a variable, therefore, the value cannot be determined in advance, so the two are not equal. From this example, we can see that the reference will point to the string buffer pool only when both sides of the + connection are string constants, all are directed to other addresses in the memory.
public static void main(String[] args) { String Monday = Monday; String Mon = Mon; final String day = day; System.out.println(Monday == Mon + day); System.out.println(Monday == Mon + day); }
Output result: true
True
Analysis: after final is added, day becomes a constant. Therefore, the reference in the second sentence is also a string buffer pool.
Appendix
Java source code for String a = new String ("abc"); implementation of this Constructor
public String(String original) { this.value = original.value; this.hash = original.hash; }