Java string buffer pool concept example

Source: Internet
Author: User

The question of String object construction is often encountered in the oral test. For example, string S = new string ("A"); creates several objects.

The following code explains the construction of various strings.

The Code is as follows:

public class TestConstructString {      public static void main(String args[])      {          String s1 = "a";          String s2 = "b";          String s3 = "ab";                    String s4 = "ab";          System.out.println("s3==s4? "+ (s3==s4));            String s5 = "a"+"b";          System.out.println("s3==s5? "+ (s3==s5));                    String s6 = s1+s2;          System.out.println("s3==s6? "+ (s3==s6));                    String s7 = new String("ab");          System.out.println("s3==s7? "+ (s3==s7));                    final String s8 = "a" ;           final String s9 = "b" ;          String s10 = s8 + s9;          System.out.println("s3==s10? "+ (s3==s10));          }  } 


Output result:

s3==s4? trues3==s5? trues3==s6? falses3==s7? falses3==s10? true


Make a simple prefix for string before explaining it.

Baidu Encyclopedia: a string in Java. The string class is unchangeable. Any change to the string class will return a new string class object. The string object is an ordered set of system. Char objects, used to represent strings. The value of the string object is the content of the ordered set, and the value is unchangeable.

Note: The string class is immutable (Final). Any change to the string class will return a new string class object. in this way, the reference of the string class is passed to a method. Any change of the method to the string has no effect on the object pointed to by the original reference. This is similar to the basic data type.

String pool: string is unchangeable. To improve efficiency, Java references the concept of string pool, such as new string ("ABC "); first, an object "ABC" will be created in the string pool. Because there is new, the content of the copystring pool will be allocated to the address space. If a String object does not exist in the string pool, it is created in the string pool.

S3 and S4 both point to the address in the same buffer pool according to the concept of string, so the result is true.

Because the sum of S3 and S5 is constants, the compiler optimizes S5 = "A" + "B" to S5 = "AB ". Therefore, the result is true.

The compiler cannot be optimized because S3 and S6 are two variables added. S1 + S2 is equivalent to (New stringbuilder (string. valueof (S1 ))). append (S2 ). tostring (); At runtime, a new string address space is allocated, instead of pointing to "AB" in the buffer pool ". Therefore, the result is false.

S3 and S7 are actually allocated address space when they are new according to the definition of the buffer pool. S7 points to the newly allocated address space, so it is false because it is different from the buffer pool address.

S3 and S10, similar to S3 and S5, are the same as they are optimized by final compilers.

There are many ways to create strings, and there are three types of return:

First, use the new keyword to create a string, such as string S1 = new string ("ABC ");

Second, directly specify. For example, string S2 = "ABC ";

Third, concatenate a new string. For example, string S3 = "AB" + "C ";

Create a String object

The key to creating a String object is to understand its principles.

Principle 1: when using any method to create a String object S, Java Runtime (running JVM) will hold this X to find whether there is a string object with the same content in the string pool, if not, a string S is created in the pool. Otherwise, it is not added in the pool.

Principle 2: in Java, as long as the new keyword is used to create an object, a new object will be created (in the heap or stack.

Principle 3: If you create a String object by specifying it directly or using a String concatenation, the system only checks and maintains the string in the string pool. If no string exists in the pool, it creates one in the pool! However, the string object will not be created in the stack area.

Principle 4: Use an expression containing variables to create a String object. This will not only check and maintain the string pool, but also create a String object in the stack area.

In addition, the intern () method of string is a local method, defined as public native string intern (); the value of Intern () method is to allow developers to concentrate their attention on the string pool. When the intern method is called, if the pool already contains a string that is the string object (the object is determined by the equals (object) method), the string in the pool is returned. Otherwise, add the string object to the pool and return
String object reference.

Finally, we have several questions: string a; is equivalent to string a = NULL when used as a class variable, but different from local variables. Null indicates an empty reference. String A = NULL indicates that A is declared in the stack, but this A does not point to any address. At this point, we noticed that string a declares a in the stack, but it does not point to any address. However, Java syntax check assumes that string a; in local variables cannot be directly used, this a in string a = NULL can be used directly.

In short:

// Case 1 string S1 = "S"; // an object is created; // case 2 S1 = new string ("S "); // two objects are created. // Condition 3: String S2 = new string ("A" + S1); // three objects are created.
Click here to view the original text

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.