Research on string and stringbuffer

Source: Internet
Author: User

First of all, please take a look at the following output results. Please think carefully and do not answer them so quickly!

 
String STR = new string ("AAA"); string str2 = new string ("AAA"); system. out. println (STR = str2); system. out. println ("----------------"); string str3 = "BBB"; string str4 = "BBB"; system. out. println (str3 = str4); system. out. println ("----------------"); string str5 = new string ("CCC"); string str6 = "CCC"; system. out. println (str5 = str6); system. out. println ("----------------"); string S = "hello"; string S1 = "El"; string S2 = "Lo"; system. out. println (S = S1 + S2); system. out. println ("----------------"); system. out. println (S = "El" + "Lo ");

Here I believe you have made the answer. The correct result is:

False True False false true

 

Next, let's take a closer look at the first one.

String STR = new string ("AAA"); string str2 = new string ("AAA"); system. Out. println (STR = str2 );

When reading this question, I hope you can take a look at the following theoretical description.

 

String S = new string ("AAA ");

1) First, check whether the string object "AAA" is available in the string pool. If yes, create the "AAA" object instead of the string pool, directly in the heap) create an "AAA" String object, return the address of the "AAA" object in the heap, and assign it to s reference, as a result, s points to the "AAA" String object created in the heap.

2) If no, create an "AAA" object in the string pool, and create an "AAA" object in the heap, then, return the address of the "AAA" object in the heap and assign it to s reference. As a result, s points to the "AAA" object created in the heap.

After reading the above description, I believe you should understand that STR and str2 references point to different objects in the heap, so the address is different and the result is false.

Then the second

 
String str3 = "BBB"; string str4 = "BBB"; system. Out. println (str3 = str4 );

Before that, let's take a look at a theoretical description:

 

String str3 = "BBB"; (assign a value using the nominal value)

1) check whether the "BBB" object exists in the string pool. If it does not exist, create a "BBB" object in the string pool, then, return the address of the "BBB" object in the string pool and assign it to the reference variable str3, so that str3 will point to the "BBB" String object in the stringpool.

2) If yes, no object is created. The "BBB" object address in the string pool is directly returned and assigned to str3 for reference.

After reading this, I don't need to talk about it. The str3 and str4 references the same object address and the result is true.

Then there is the third one. I believe that if you understand the first two, the third one will not need to be said. If you do not understand, please first understand the first two.

Now let's look at the fourth

 
String S = "hello"; string S1 = "El"; string S2 = "Lo"; system. Out. println (S = S1 + S2 );

When we look at this issue, we will also look at a theoretical description:

String is a constant and its object cannot be changed once it is created. When you use + to splice strings, A New String object is generated instead of appending content to the original string object.

Therefore, when S1 + S2 is used, a new object is created in the heap, so the address of S is different from that of S1 + S2.

For the last question

System. Out. println (S = "El" + "Lo ");

In my understanding, the JVM connects the "+" Number of string constantsProgramCompilation phase,

The JVM optimizes the "+" connection of the constant string to the connected value. Take "El" + "Lo" for example, after the compiler is optimized, it is already "hello" in the class.

During compilation, the value of its String constant is determined and stored in the String constant pool. Therefore, the final result of the above program is true.

The following is an additional question about stringbuffer:

 
Stringbuffer SB1 = new stringbuffer ("hello"); stringbuffer sb2 = new stringbuffer ("hello"); system. Out. println (sb1.equals (sb2 ));
 
Don't say "true". The equals method is not rewritten in stringbuffer. Therefore, the equals method of the object is used. Therefore, they compare the addresses and the result is false.

Summary:If you can understand the above questions, I believe you have a deeper understanding of the string class. Let's summarize the following:

The stack stores local variable data of some original data types and references of objects (string, array. Object, etc.) but does not store object content.

Heap stores the objects created with the new keyword.

A string is a special packaging class. Its reference is stored in the stack, and the object content must be determined according to the creation method (constant pool and heap ). some have been created in the compilation phase and stored in the regular string pool, while others are created during runtime. when the new keyword is used and stored in the heap, We need to judge based on the situation. In fact, if we understand the principle, all similar problems will be solved.

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.