1. The role of the final keyword
1.1, Final decorated class: Cannot be inherited by another class;
1.2, final Modified method: cannot be overridden by a quilt class
1.3, final modified variables: In two cases, reference variables, basic type variables, as follows:
final decorated reference variable: once assigned, it cannot be re-assigned
Final // reference variable s = "1"; /** Error: The final local variable s cannot be assigned. It must is blank and not using a compound assignment* /
Final-Modified Basic type variable: Once you assign it, you cannot change its value
Final int // basic type i = 2; /** Error: The final local variable s cannot be assigned. It must is blank and not using a compound assignment* /
2. The addition of string strings
Example 1:
1 String str1 = "a"; 2 String str2 = "B"; 3 String STR3 = "AB"; 4 String STR4 = str1 + str2; 5 // output results? 6 String STR5 = str4.intern (); 7 // output results?
The first line: "A" is stored as a constant in the string pool, and its reference address is assigned to the STR1;
the second line: "B" is stored as a constant in the string pool, and its reference address is assigned to the STR2;
The third line: "AB" is stored as a constant in the string pool, and its reference address is assigned to the STR3;
Line Fourth: str1 + str2, when the + number on both sides of the variable (on either side or any of the sides), in the compilation period is unable to determine its value, so wait until the run time to process again, Processing method: First new A StringBuilder, then append str1 and str2, so: The STR4 reference variable points to a new object in the heap, whereas the STR3 reference variable points to the constant "AB" in the string pool 's address;
Line Fifth: Str3 = = STR4 Comparison of STR3 and STR4 reference address, so false;
Line Sixth: The Intern () method of string transfers the value of the object to the string pool, if the string pool already has the same value, the address is returned directly, if not, create a new one in the string pool and return the address. Looking at this example, it is clear that "AB" already exists in the string pool (the third row), so both STR5 and Str3 point to "AB" in the string pool, and the syntax STR3 = = STR5 comparison is a reference address , so the output junction output is: true;
Note: Intern () This method API explains: "When theintern method is called, if the pool already contains a string equal to this string object (as determined by the Equals (Object) method), the string in the pool is returned. Otherwise, this string object is added to the pool and a reference to this string object is returned. "
Example 2:
1 String str1 = "AB"; 2 String str2= "a" + "B"; 3 System.out.println (str1 = = str2); // output results?
The first line: "AB" is stored as a constant in the string pool, and the reference address is assigned to STR1;
The second line: "A" and "B" as two constants added, due to compiler optimization, in the compilation period will be + both sides splicing merge, directly considered as a constant "AB", the results found that the string pool has a constant "AB", the "AB" is directly assigned to the reference address str2;
The third line: str1 = = str2 Comparison of the str1 and str2 of the reference address, of course, the same, so output result: true;
Example 3:
1 Final String str1 = "a"; 2 Final String str2 = "B"; 3 String str3 = "AB"; 4 String STR4 = str1 + str2; 5 System.out.println (STR3 = = STR4); // output results?
The first line: "A" is stored as a constant in the string pool, and its reference address is assigned to the STR1; final causes the variable STR1 to be treated as a constant
the second line: "B" is stored as a constant in the string pool, and its reference address is assigned to the STR2; final causes the variable STR2 to be treated as a constant
The third line: "AB" is stored as a constant in the string pool, and its reference address is assigned to the STR3;
Line Fourth: Str1 + str2, at this time the + sign both sides are constant Oh, at the compile time can determine its value
that is: String STR4 = str1 + str2;
is equivalent to String STR4 = "a" + "B";
is equivalent to String STR4 = "AB";
Line Fifth: STR4 points to the constant "AB", str3 points to the constant "AB" (the third row), so str3 = = Str4 equals True
so now consider a small question: if the final of the first line is removed, is the output true or false? If you understand the above, this problem should not be difficult
Tip: If you don't know the answer, consider the fourth line of Example 1.
Java Foundation 001:final keyword, string string addition problem