Java record-26-StringPool and field assignment

Source: Internet
Author: User

Java record-26-StringPool and field assignment
The String class in Java is frequently used during development, so JDK developers have made many optimizations to the String class. Java maintains the concept of a StringPool string pool. In Java, the literal value assignment of the String class is different from the new value assignment. 1. String a1 = "bb"; String a2 = "bb"; System. out. println (a1 = a2); the output above is true. Why? StringPool (character string pool). For strings, Java maintains the concept of a string pool. When a string is directly assigned a value using a literal, two things are actually done: First, check whether there is any string in the string pool that contains the string content, if the string does not exist, it is placed in the string pool, and a string object is created to return the object address to the reference. When the string is assigned again, the string is determined to exist in the string pool again, if an object exists, the address is directly returned to the reference instead of creating an object. In this way, multiple references point to the same object. 2. string s = "aaa"; to assign a value using the literal value, perform the following steps: 1 ). check whether the "aaa" object exists in the StringPool. If it does not exist, create an "aaa" object in the StringPool and return the "aaa" address in the StringPool, assign the reference variable s, which points to the "aaa" String object in the StringPool. 2). If no object is created, the "aaa" object address in the StringPool is directly returned and assigned to s reference. 3. string s1 = new String ("aa"); String s2 = new String ("aa"); System. out. println (s1 = s2); System. out. println (s1.equals (s2); in the above Code output, the first is false, and the second is true. The second value is true. As mentioned previously, the equals of the String class compares the content of the two strings. But the first one is false. Didn't the StringPool mentioned above be used? The main areas of memory are heap and stack. The StringPool string pool is in the java stack, and the new objects are all in the java heap. 4. string s = new String ("aaa"); // String when using new to create an object, StringPool1 is also used ). first, the StringPool will look for the existence of the string object "aaa". If yes, the StringPool will no longer create the object "aaa, instead, an "aaa" String object is directly created in the heap, and the address of the object created in the heap is returned to s for reference, as a result, s points to the "aaa" String object created in the heap. 2 ). if it is not stored, an "aaa" String object will be created in the StringPool first, and then an "aaa" String object will be created in the heap, in addition, the address of the object created in the heap is returned to s reference, causing s to point to the "aaa" String object created in the heap. 5. When using new to create a string, no matter whether the string object exists in the StringPool or not, this string object will be created in the heap and the return address will be referenced. Therefore, all string objects created by new point to the heap address. String is very troublesome. For different JDK implementations, the implementation of String is also different.

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.