Several examples of String constant pool problems
The following is a comparative analysis and understanding of several common examples:
- String A = "A1"
;
- String B = ""
+ 1
;
- System. Out. println (A = B); // result = true
- String A = "atrue"
;
- String B = ""
+ "True"
;
- System. Out. println (A = B); // result = true
- String A = "a3.4"
;
- String B = ""
+ 3.4
;
- System. Out. println (A = B); // result = true
Analysis: JVM connects the "+" Number of string constants. During the program compilation period, the JVM optimizes the "+" connection of the constant string to the connected value, take "A" + 1 as an example. After the compiler is optimized, it is already A1 in the class. The value of its String constant is determined during compilation, so the final result of the above program is true.
- String A = "AB"
;
- String BB = "B"
;
- String B = ""
+ BB;
- System. Out. println (A = B); // result = false
Analysis: JVM references strings. Due to the existence of string references in the "+" connection of strings, the referenced values cannot be determined during program compilation, that is, "a" + BB cannot be optimized by the compiler. It is dynamically allocated only during the running period and assigned the new connection address to B. Therefore, the result of the above program is false.
- String A = "AB"
;
- Final
String BB = "B"
;
- String B = ""
+ BB;
- System. Out. println (A = B); // result = true
Analysis: The only difference from [3] Is that the BB string is decorated with final. For final modified variables, it is parsed as a local copy of a constant value during compilation and stored in its own constant pool or embedded in its byte code stream. Therefore, "a" + BB "and" A "+" B "have the same effect. Therefore, the result of the above program is true.
- String A = "AB"
;
- Final
String BB = getbb ();
- String B = ""
+ BB;
- System. Out. println (A = B); // result = false
- Private
Static
String getbb (){
- Return
"B"
;
- }
Analysis: JVM cannot determine the value of BB in string reference during compilation. Only after the method is called during the runtime, dynamically connect the return value of the method with "A" and assign the address B. Therefore, the result of the above program is false.
The above four examples show that:
String S = "A" + "B" + "C ";
It is equivalent to string S = "ABC ";
String A = "";
String B = "B ";
String c = "C ";
String S = A + B + C;
This is different. The final result is equal:
- Stringbuffer temp =New
Stringbuffer ();
- Temp. append (a). append (B). append (C );
- String S = temp. tostring ();
From the above analysis results, it is not difficult to infer the cause of the inefficiency of the string using the join operator (+), such as the Code:
- Public
Class
Test {
- Public
Static
Void
Main (string ARGs []) {
- String S =Null
;
- For
(Int
I = 0
; I <100
; I ++ ){
- S + = ""
;
- }
- }
- }
Every time you do the plus sign (+), a stringbuilder object is generated and then thrown away after append. When the next loop arrives, A stringbuilder object is generated again, and then the append string is generated. The loop ends until the end. If we directly use the stringbuilder object for append, we can save n-1 time to create and destroy objects. Therefore, for applications that require string connection in a loop, the append operation is generally performed using the stringbuffer or stringbulider object.
The intern method of the string object is described as follows:
- Public
Class
Test4 {
- Private
Static
String A = "AB"
;
- Public
Static
Void
Main (string [] ARGs ){
- String S1 = ""
;
- String S2 = "B"
;
- String S = S1 + S2;
- System. Out. println (S = A); // false
- System. Out. println (S. Intern () = A); // true
- }
- }
Java
It is a constant pool problem. For the S1 + S2 operation, a new object is actually created in the heap, and s stores the content of the new object in the heap space, the values of S and a are not equal. When the S. Intern () method is called, the address value of S in the constant pool can be returned. Because the value of A is stored in the constant pool, the values of S. intern and a are equal.
Summary
The stack stores local variable data of some original data types and references of objects (string, array. Object, etc.) but does not store object content.
Heap stores the objects created with the new keyword.
A string is a special packaging class. Its reference is stored in the stack, and the object content must be determined according to the creation method (constant pool and heap ). some have been created in the compilation phase and stored in the regular string pool, while others are created during runtime. store the new keyword in the heap.