Understanding Java string (non-original)

Source: Internet
Author: User

To understand how string works in Java, you must make it clear that string is an immutable class ). What is a non-mutable class? Simply put, an instance of a non-mutable class cannot be modified. The information contained in each instance must be provided at the time of creation, and remains unchanged throughout the lifecycle of the object. Why does Java design string as a non-mutable class? You can ask James Gosling :). But the non-variable class does have its own advantages, such as single state, simple object, easy to maintain. Second, such object objects are essentially thread-safe and do not require synchronization. In addition, users can share mutable objects or even their internal information. (For details, see objective Java item 13 ). The string class is widely used in Java and even has its presence in class files. Therefore, it is appropriate to design it as a simple and lightweight non-variable class.
1. Create.
Now that we know that string is a non-mutable class, we can further understand the string construction method. To create a stirng object, you can use either of the following methods:

Java code
1. String str1 = new string ("ABC ");
2. stirng str2 = "ABC ";
Although both statements return a reference to a string object, the JVM treats the two statements differently. For the first type, JVM will immediately create a String object in heap, and then return the reference of this object to the user. For the second type, JVM first searches for whether the string object is stored in the object pool in the strings pool maintained internally using the string equels method. If yes, the existing String object is returned to the user, instead of re-creating a new String object in heap. If the string object does not exist in the object pool, the JVM creates a new String object in heap, return the reference to the user and add the reference to the strings pool. Note: When you use the first method to create an object, JVM will not take the initiative to put the object in the strings pool unless the program calls the intern method of string. See the following example:

Java code
1. String str1 = new string ("ABC"); // JVM creates a String object on the stack.
2.
3. // JVM cannot find a string with the value of "ABC" in strings pool. Therefore
4. // create a String object on the stack and add the reference of this object to the strings pool.
5. // There are two string objects on the stack.
6. stirng str2 = "ABC ";
7.
8. If (str1 = str2 ){
9. system. Out. println ("str1 = str2 ");
10.} else {
11. system. Out. println ("str1! = Str2 ");
12 .}
13. // The print result is str1! = Str2, because they are two different objects on the stack
14.
15. String str3 = "ABC ";
16. // at this time, JVM finds that there is an "ABC" object in the strings pool, because "ABC" equels "ABC"
17. // Therefore, the objects directed to str2 are directly returned to str3, that is, str2 and str3 point to the reference of the same object.
18. If (str2 = str3 ){
19. system. Out. println ("str2 = str3 ");
20.} else {
21. system. Out. println ("str2! = Str3 ");
22 .}
23. // print the result as str2 = str3
Let's look at the following example:

Java code
1. String str1 = new string ("ABC"); // JVM creates a String object on the stack.
2.
3. str1 = str1.intern ();
4. // The program explicitly places str1 In the strings pool. The intern running process is as follows: first view the strings pool
5. // if there is no reference to the "ABC" object, no, create an object in the heap and add the reference of the new object
6. // strings pool. After executing this statement, str1's original string object has become a spam object and will
7. // collected by GC.
8.
9. // at this time, JVM finds that there is an "ABC" object in the strings pool, because "ABC" equels "ABC"
10. // Therefore, the object directed by str1 is directly returned to str2, that is, str2 and str1 reference the same object,
11. // at this time, there is only one valid object on the stack.
12. stirng str2 = "ABC ";
13.
14. If (str1 = str2 ){
15. system. Out. println ("str1 = str2 ");
16.} else {
17. system. Out. println ("str1! = Str2 ");
18 .}
19. // The print result is str1 = str2
20.

Why can JVM process string objects like this? It is because of the non-variability of the string. Since the referenced object never changes once it is created, multiple references share one object without affecting each other.

2. concatenation ).
Java programmers should know that misuse of String concatenation operators will affect program performance. Where does the performance problem come from? In the final analysis, it is the non-variability of the string class. Since the string object is non-mutable, that is, once an object is created, it cannot change its internal State. However, the concatenation operation obviously requires the growth of the string, that is, to change the internal status of the string, there is a conflict between the two. What should we do? To maintain the non-variability of the string, you have to create a new String object after the concatenation is complete to represent the new string. That is to say, every time a string operation is executed, new objects will be generated. If the string operation is executed frequently, a large number of objects will be created, and performance problems will arise.
To solve this problem, JDK provides a variable supporting class for the string class, stringbuffer. The stringbuffer object is variable. The internal data structure is changed only when the string is connected, but no new object is created. Therefore, the performance is greatly improved. For a single thread, JDK 5.0 also provides the stringbuilder class. In a single-threaded environment, this class can further improve the performance because synchronization is not required.

3. Length of string
We can use the concatenation operator to get a long string. How many characters can a String object contain at most? View the source code of the string, we can know that the string class uses the Count field to record the number of object characters, and the count type is int. Therefore, we can infer that the longest length is 2 ^ 32, that is, 4G.
However, when writing the source code, if you define a string in the form of sting STR = "aaaa";, the ASCII characters in double quotation marks can only contain 65534. Why? In the class file specification, the constant_utf8_info table uses a 16-bit unsigned integer to record the length of the string. The maximum length can be 65536 bytes, while the Java class file uses a variant of the UTF-8 format to store characters, the null value is expressed in two bytes, so only 65536-2 = 65534 bytes are left. It is also the reason for the variant UTF-8, if the string contains non-ASCII characters such as Chinese, then the number of characters in double quotes will be less (a Chinese character occupies three bytes ). If this number is exceeded, the compiler reports an error during compilation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.