What to know about String
-
Basic features of String
-
How memory is allocated
-
Field splices
-
intern()
Basic features of String
Immutable feature of the existence of the original existence of the object can not be modified, is the constant pool of meaning, I understand ah.
Example:Print result:
good
best
Ex. STR will create a new “test OK” object, but you must have another question:
Why doesn’t the StringExer guide to the Str object change with the assignment in the change method?
The change function is run as a stack frame, and the argument is held in a local variable. A change to STR is a change to STR in a local variable table, not a change to STR in a stringExer object, so STR in a stringExer object remains unchanged. I hope I cleared that up.
Constant pool
Constant pool =HashTable (hash array + linked list structure), the existence of string constant pool can avoid the same string created in this way, save space.
jdk6 | jdk7 | |
---|---|---|
Constant pool location | Methods area | The heap |
String. Intern () Specifies whether to create an object in the constant pool | New objects will be created because they are stored in different locations | No, a direct reference to the previous object address |
String memory allocation
1, directly using double quotation marks: stored in the constant pool
2, new String() declaration: exists in the heap, string.intern() after the location and whether to create a new object see the table above constant pool.
Field splices
You can decompile the class file to see the essence of “+” :
1. “ab “+” CD “: optimized at compile time to become “abcd”.
2, the String a = “ab”; String b=”cd”; String c=a+b; : Variables are processed through StringBuilder, and toString() at the end produces a new object.
3. If the variables A and B are final, they are still in the first case.
intern()
The objectstring () method returns the address of the String in the constant pool if it already exists, or if it does not, returns the address. This requires a separate JDK version, which you can see in the table above.
The guarantee variable indicates the data in the string constant pool:
1, String a =” ABC “;
2, the String a =… .intern(); … Represents any form, such as new String();
The interview questions
How many objects do I create? :
1, String STR = new String(“ab”);
Answer: two objects: one object “ab” in the string constant pool, (bytecode instruction: LDC)
A String that comes out of a new in the heap.
String c =new String(“a”) + new String(“b”);
1, new String(“..”) ) * 2 = 4.
2, StringBuffer a.
3, stringBuffer.toString () one.
Exercises:
Brief description: if s3 == S4, see here: s3.intern(); Generate “11” in the string constant pool,
Jdk6 will create a new object “11” in the method area and will have the new address.
Instead of creating an “11” object in the String constant pool, jdk7 creates an address that points to the new String object before the heap space.
S3 == S4
Results: the false; The.intern() method is executed in a different order, resulting in different addresses for the two objects.
Summary of intern()
The existence of string constant pool can prevent the same string from being created in this way, saving space. So the correct use of the intern() method can be very effective (for example, in cases where there is a lot of repeated string object creation)
\