Java String Buffer pool analysis

Source: Internet
Author: User

Java's virtual machines create a separate area in memory that is used to store string objects, a memory area called a string buffer pool. How does the Java string buffer pool work?

String"abc";String"abc";StringnewString("xyz");

For example, the upper code:
String a = "abc";

When creating a string, find out if there are no identical objects in the string buffer pool, if the same object returns a reference to the object directly, create the object in the string buffer pool without the same object, and then return the object's app. For this step, there is no ABC string object in the buffer pool, so first create a string object and then return the object reference to a.

String B = "abc";

This sentence is also intended to create an object that references variable B to an object that points to ABC. At this point, first find the string buffer pool, found that the ABC object has already, this is the object directly to the reference to B, at this time A and B shared an object ABC, but do not worry, a changed the string and affect B, because the string is a constant, once created there is no way to modify, Unless a new object is created.

String c = new String ("XYZ"); (the implementation of this construction method is shown in the appendix)

The Find string buffer pool found no XYZ string object, so it creates a Zyx object in the string buffer pool and returns the reference.

As can be seen from the above analysis, when new a string is not necessarily created a new object, it is possible that the same object is used in conjunction with other reference variables. Here are a few common questions about string buffer pools.

How many string objects have been created

        String"abc";        String"abc";        StringnewString("xyz");        StringnewString("xyz");        String e="ab"+"cd";

This program is similar to the above program, we compare to look at:

String a = "abc"; because there is no ABC string object in the buffer pool, an object is created; string b = "abc"; because ABC is already in the buffer pool, no new objects will be created; string c = new String (" XYZ "), because there is no XYZ string object, the object of XYZ is created first, and then the string object is created as a string by constructing a new string object in memory (not the buffer pool), thus creating a total of two objects; string d = new String ("xyz"), omitting the process of creating an object, so that only one object is created, string e= "AB" + "CD", because the value of the constant is determined at compile time. So this sentence is equivalent to string e= "ABCD"; an object was created

So the number of objects created is: 1,0,2,1,1

exactly equal or unequal

When we learn Java, we know that two string objects equal judgment to use equal instead of using = =, but after learning the string buffer pool, you should know why can not use = =, under what circumstances = = and equal is equivalent, first of all, must know, = = Comparing the memory addresses of two objects is equal, let's take a look at a few programs:

publicstaticvoidmain(String[] args) {         "Monday";         "Monday";         if (s1 == s2)             System.out.println("s1 == s2");         else             System.out.println("s1 != s2");     }

Output result: S1 = = S2
Analysis: By introducing the string buffer pool above, we know that both S1 and S2 are pointing to the same object in the string buffer pool, so the memory address is the same, so use = = to determine whether two strings are equal.

 Public Static void Main(string[] args) {String S1 ="Monday"; String s2 =NewString ("Monday");if(S1 = = s2) System. out. println ("S1 = = S2");ElseSystem. out. println ("S1! = S2");if(S1.equals (S2)) System. out. println ("S1 equals S2");ElseSystem. out. println ("S1 not equals S2"); }

Output: S1! = S2
S1 equals s2
Analysis: From the above analysis we know that string s2 = new String ("Monday"); This sentence does not create a new object in the string buffer pool, but creates a new object in another location in memory, so S1 is pointing to the string buffer pool. S2 are other locations that point to memory, and the memory addresses of the two are different.

 Public Static void Main(string[] args) {String S1 ="Monday"; String s2 =NewString ("Monday"); S2 = S2.intern ();if(S1 = = s2) System. out. println ("S1 = = S2");ElseSystem. out. println ("S1! = S2");if(S1.equals (S2)) System. out. println ("S1 equals S2");ElseSystem. out. println ("S1 not equals S2"); }

Output result: S1 = = S2
S1 equals s2
Analysis: First of all, the function of intern () This method is to return the reference to the object in the string buffer pool, so S2 points to the address in the string buffer pool, and S1 is equal.

publicstaticvoidmain(String[] args) {         "Monday";          "Mon";          String  "day";          System.out"Mon""day");          System.out"Mon" + day);      }

Output Result: True
False
Analysis: The first why is equal to true we have said, because both are constants so in the compilation phase can be determined, in the second, day is a variable, so can not determine his value in advance, so the two are not equal, from this example we can see that only the + connection on both sides are string constants, References point to a string buffer pool, all pointing to other addresses in memory.

publicstaticvoidmain(String[] args) {         "Monday";          "Mon";          final String  "day";          System.out"Mon""day");          System.out"Mon" + day);      }

Output Result: True
True
Analysis: Plus final day becomes a constant, so a reference to the second sentence is also a pointer to a string buffer pool.

Appendix
Java source code for string a = new string ("abc"), the implementation of this construction method

publicString(String original) {        this.value = original.value;        this.hash = original.hash;    }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java String Buffer pool analysis

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.