Let's first look at a piece of code:
Java code:
String STR = new string ("ABC ");
This problem is often followed by this Code, that is, how many string objects have been created in this line of code? Phase
Xin's family is no stranger to this question, and the answer is also well known, two. Next, let's start with this question.
Review some java knowledge related to creating string objects.
We can divide the above line of code into four parts: String STR, =, "ABC", and new string. String STR only
Is to define a string type variable named STR, so it does not create an object; = is to start the variable Str
First, assign the reference (or handle) of an object to it. Obviously, no object is created. Now only new
String ("ABC. So why can new string ("ABC") be regarded as "ABC" and new string? Let's see
The string constructor we called:
Java code
Public String (string original ){
// Other code...
}
As we all know, we often use the following methods to create a class instance (object:
1. Use new to create an object.
2. Call the newinstance method of the class and use the reflection mechanism to create an object.
We use new to call the constructor method of the string class to create an object and assign its reference
The value is given to the STR variable. At the same time, we noticed that the parameter accepted by the called constructor method is also a String object,
This object is "ABC ". Therefore, we need to introduce another method for creating string objects-quotation marks inside the package
Contains text.
This method is unique to a string and is very different from the new method.
Java code
String STR = "ABC ";
Undoubtedly, this line of code creates a String object.
Java code
String A = "ABC ";
String B = "ABC ";
What about this? The answer is still one.
Java code
String A = "AB" + "cd ";
What about this? The answer is still one. A little strange? Here, we need to introduce relevant knowledge about the string pool.
A review of the knowledge.
There is a string pool in the Java Virtual Machine (JVM), which stores many string objects and can be
Shared usage, which improves efficiency. Because the string class is final, its value cannot be changed once it is created, so
We don't have to worry about program chaos caused by string object sharing. The string pool is maintained by the string class. We can call
Intern () method to access the string pool.
Let's look back at string a = "ABC"; when this line of code is executed, the Java VM first queries it in the string pool
Find whether such an object with the value of "ABC" already exists. Its judgment is based on the string equals (Object OBJ) side.
Method. If yes, a reference to an existing object is directly returned without creating a new object. If no
Create this object, add it to the string pool, and return its reference. Therefore, it is not difficult to understand the previous
The first two examples in the three examples are the answer.
For the third example:
Java code
String A = "AB" + "cd ";
The constant value is determined during compilation. Here, both "AB" and "cd" are constants, so the value of variable A is
It can be determined during compilation. After this line of code is compiled, the effect is equivalent:
Java code
String A = "ABCD ";
Therefore, only one object "ABCD" is created and saved in the string pool.
Now again, is it true that all strings obtained after the "+" connection will be added to the string pool? Me
We all know that "=" can be used to compare two variables. It has the following two situations:
1. If two basic types (char, byte, short, Int, long, float, double, Boolean) are compared ),
Is to judge whether their values are equal.
2. If the table has two object variables, it is used to determine whether their references point to the same object.
Next we will use "=" for several tests. For ease of description, we point to an existing object in the string pool.
It is also considered that the object is added to the string pool:
Java code
Public class stringtest {
Public static void main (string [] ARGs ){
String A = "AB"; // creates an object and adds it to the string pool.
System. Out. println ("string a =/" AB /";");
String B = "cd"; // creates an object and adds it to the string pool.
System. Out. println ("string B =/" CD /";");
String c = "ABCD"; // creates an object and adds it to the string pool.
String d = "AB" + "cd ";
// If D and C point to the same object, it means D is added to the string pool.
If (D = c ){
System. Out. println ("/" AB/"+/" CD/"created object/" added to/"string pool ");
}
// If D and C do not point to the same object, it means D is not added to the string pool.
Else {
System. Out. println ("/" AB/"+/" CD/"created object/" not added to/"string pool ");
}
String E = a + "cd ";
// If E and C point to the same object, e is added to the string pool.
If (E = c ){
System. Out. println ("A +/" CD/"created object/" added to/"string pool ");
}
// If E and C do not point to the same object, e is not added to the string pool.
Else {
System. Out. println ("A +/" CD/"created object/" not added to/"string pool ");
}
String F = "AB" + B;
// If F and C point to the same object, F is added to the string pool.
If (F = c ){
System. Out. println ("/" AB/"+ B Created object/" added to/"string pool ");
}
// If F and C do not point to the same object, F is not added to the string pool.
Else {
System. Out. println ("/" AB/"+ B Created object/" not added/"string pool ");
}
String G = A + B;
// If G and C point to the same object, G is added to the string pool.
If (G = c ){
System. Out. println ("A + B created object/" added to/"string pool ");
}
// If G and C do not point to the same object, G is not added to the string pool.
Else {
System. Out. println ("the object created by a + B/" not added to/"string pool ");
}
}
}
The running result is as follows:
1. String A = "AB ";
2. String B = "cd ";
3. The "AB" + "cd" created object "is added to the" string pool"
4. The object created by a + "cd" is not added to the "string pool"
5. "AB" + the object created by B "is not added to the" string pool"
6. The object created by a + B is not added to the string pool.
From the above results, we can easily see that only strings created using quotation marks containing text use"
+ "The new object generated by the connection will be added to the string pool. For all newly created objects that contain the new method (including null)
The "+" Join expression of, and all the new objects it generates will not be added to the string pool, so we will not repeat this.
However, there is a situation that requires attention. See the following code:
Java code
Public class stringstatictest {
// Constant
Public static final string a = "AB ";
// Constant B
Public static final string B = "cd ";
Public static void main (string [] ARGs ){
// Concatenates two constants with + to initialize S.
String S = A + B;
String T = "ABCD ";
If (S = T ){
System. Out. println ("s equals T, they are the same object ");
} Else {
System. Out. println ("s is not equal to T, they are not the same object ");
}
}
}
The running result of this Code is as follows:
• S is equal to T. They are the same object.
Why? The reason is that the value of a constant is fixed, so it can be confirmed during compilation.
The variable value can only be determined at run time, because the variable can be called by different methods, so that
Can cause a value change. In the preceding example, both A and B are constants and their values are fixed. Therefore, the values of S are also fixed,
It is determined when the class is compiled. That is to say:
Java code
String S = A + B;
Equivalent:
Java code
String S = "AB" + "cd ";
I slightly changed the above example to see what will happen:
Java code
Public class stringstatictest {
// Constant
Public static final string;
// Constant B
Public static final string B;
Static {
A = "AB ";
B = "cd ";
}
Public static void main (string [] ARGs ){
// Concatenates two constants with + to initialize S.
String S = A + B;
String T = "ABCD ";
If (S = T ){
System. Out. println ("s equals T, they are the same object ");
} Else {
System. Out. println ("s is not equal to T, they are not the same object ");
}
}
}
The running result is as follows:
• S is not equal to T. They are not the same object
After a few changes, the result is exactly the opposite of the previous example. Let's analyze it again. Although A and B are defined
Constant (can only be assigned once), but none of them are assigned immediately. Before calculating the value of S, when will they
The value assigned and the value assigned are all variables. Therefore, before being assigned a value, A and B are similar
Variable. S cannot be determined during the compilation period, but can only be created at runtime.
Because the sharing of objects in the string pool can improve the efficiency, we recommend that you use quotation marks to include text.
To create a String object, which is often used in programming.
Next let's take a look at the intern () method. Its definition is as follows:
Java code
Public native string intern ();
This is a local method. When calling this method, the Java Virtual Machine first checks whether the string pool already exists
The object with the same value exists. If yes, the reference of the object in the string pool is returned. If no, the object is first referenced in the string pool.
Create a String object with the same value, and then return its reference.
Let's look at this Code:
Java code
Public class stringinterntest {
Public static void main (string [] ARGs ){
// Use the char array to initialize a to avoid the existence of a value in the string pool before a is created
Is an object of "ABCD"
String A = new string (New char [] {'A', 'B', 'C', 'D '});
String B = A. Intern ();
If (B = ){
System. Out. println ("B is added to the string pool, no new object ");
} Else {
System. Out. println ("B is not added to the string pool, new object ");
}
}
}
Running result:
• B is not added to the string pool and a new object is created.
If the intern () method of the string class does not find an object with the same value, it adds the current object to the string pool.
Then, B and a point to the same object; otherwise, B points to the java virtual
The pseudo-machine is created in the string pool, but its value is the same as that of. The running result of the above Code exactly confirms
This.
Finally, let's talk about the storage of string objects in the Java Virtual Machine (JVM), as well as the string pool and heap
(HEAP) and stack. Let's first review the difference between stack and stack:
• Stack: stores basic types (or built-in types)
(Char, byte, short, Int, long, float, double, Boolean) and object reference, data can be shared,
The speed is second only to registers (Register) and faster than the heap.
• Heap: used to store objects.
We can view the source code of the string class and find that it has a value attribute that stores the value of the string object. The type is
Char [], which also indicates that the string is the character sequence.