There is a very important rule for using strings in Java to remember that once a string object is configured, its contents are invariant. For example, the following statement:
This declaration configures a string object with a length of 11 and Caterpillar content, and you cannot change the contents of this string object. Don't assume that the following statement changes the contents of a String object:
In fact, there will be two string objects in this program fragment, one is the just string object, the length is 4; one is the Justin string object, the length is 6, and the two are different string objects. Instead of adding in strings after the just string, you have the str name referenced from the Justin string object. The concept is shown in Figure 6-2.
Figure 6-2 Using = as a string specified meaning
In Java, the use of "=" To assign a string object to a variable name, meaning to change the object referenced by the name, the original referenced string object, if no other name to reference it, will be in due time by Java "garbage collection" (Garbage Collection) Mechanism recovery. In Java, programmers do not usually care about the problem of resource deallocation for unwanted objects, and Java checks that objects are no longer referenced and that objects that are not referenced by any name will be reclaimed.
A string pool is maintained while Java is executing. For some of the string objects that can be shared, it is first found in the string pool whether the same string content (characters are the same), if there is a direct return, rather than directly create a new string object, to reduce memory consumption. If you declare it in your program by using the following method, you are actually pointing to the same string object:
String str1 = "Flyweight";
System.out.println (str1 = = str2);
When you use "" to include a string directly in your program, the string is in a string pool, as is the case with flyweight in the program fragment above. Flyweight will have an instance in the program, while STR1 and STR2 are both referenced from flyweight. Figure 6-3 shows the concept.
Figure 6-3 String variable str1, str2 reference from flyweight
In Java, if = = is used in a variable of two object types, it is used to compare whether two variables are referenced from the same object, so in Figure 6-3 when STR1==STR2 is compared, the execution result of the program shows true.
To take a look at the Intern () method for string, first look at the excerpt from the API Description (translation):
When the Intern () method is invoked, if the same string object is already included in the pool (the same or not by the Equals () method), the string is returned from the pool, otherwise the original string object is added to the pool and a reference to the string object is returned.
This passage actually illustrates how the flyweight mode (see the network index in this chapter) works, and the direct use of the example 6.4来 explanation will be clearer.
• Example 6.4 Internstring.java
public class Internstring {
public static void Main (string[] args) {
String str1 = "Fly";
String str2 = "weight";
String STR3 = "Flyweight";
String STR4 = null;
STR4 = str1 + str2;
System.out.println (STR3 = = STR4);
STR4 = (str1 + str2). Intern ();
System.out.println (STR3 = = STR4);
}
}
Style= ' font-family: Arial ' > Using graphics to illustrate this example, after declaring str1, str2, and STR3, the status in the String pool is shown in Figure 6-4.
Figure 6-4 String Pool schematic
In Java, using the + concatenated string produces a new string object, so the first time you compare STR3 with a STR4 object in a program, the result is false, as shown in Figure 6-5.
Figure 6-5 String addition produces a new string
The Intern () method first checks whether a string object with the same character part exists in the string pool, and returns if there is one. Because the flyweight string object was previously declared in the program, Intern () was found in the string pool, so it is returned directly. Then again, STR3 and STR4 are pointing to the same object, so the result will be true, as shown in Figure 6-6.
Figure 6-6 Intern () returns a reference to the string object in the string pool
The above note also draws a conclusion that = = is used in Java to compare whether two variable names are referenced from the same object, so you cannot use = = to compare the character content of two strings is the same. For example:
String str1 = new String ("Caterpillar");
String str2 = new String ("Caterpillar");
System.out.println (str1 = = str2);
Although the character values of the two string objects are identical, in fact, in this program fragment, two string instances are produced, str1 and str2 are referenced separately from different instances. So when you use = = comparison, the results are displayed false. If you want to compare the character values of two string objects to the same value, use the Equals () method, and the following methods will display the result of true:
String str1 = new String ("Caterpillar");
String str2 = new String ("Caterpillar");
System.out.println (Str1.equals (str2));