Java create string object mechanism string buffer pool string concatenation mechanism

Source: Internet
Author: User
Tags true true

For the mechanism of creating a string object, what is involved in this process is worth exploring.

Let's look at the following code to see how the new String object differs from the direct assignment:

 Public Static voidMain (string[] args) {String str1=NewString ("ABC"); String str2="ABC"; String STR3=NewString ("ABC");
String STR4 = "abc"; System. out. println (str1 = =str2); System. out. println (str1 = =STR3); System. out. println (str2 = =STR3);
System.out.println (str2 = = STR4);}

The result is: false false true

We know that = = comparison is the object reference, from the code as well as the results can be seen in this program only three objects, str1 point to an object, Str3 point to an object, str2 and STR4 together point to an object. But some of the students here will be confused, we know that in Java, the string class is final modified is immutable should be rebuilt every time an object ah. But in this case the object content is "ABC", so the program itself has not changed. If a change occurs, the object is not necessarily regenerated. This is related to the string buffer pool in the string mechanism.

When you create a string object with new, you will first find an object in the string buffer pool that is equal to the content of the newly created string, and if not, create a new string object in the buffer pool and then create a string object in the heap. If an object in the buffer pool already has the same content as the newly created string, the object is created directly on the heap. If you don't use new, you'll see if you've created the object in the buffer pool, and if you haven't created it, create one in it, and if you've already created the new declaration, it will point directly to the object.

So the first line of the main method is execution, and there is no object in the buffer pool that is "ABC", so the object content is "ABC" in the buffer pool, and an object is created in the heap. Execution to the second row will still want to go to the buffer pool to find whether the content is "ABC" string object, found already. Because he doesn't have to create objects on the heap, he points the str2 directly to the objects in the buffer pool. The third line is the same reason the buffer pool is already there so just create a new one on the heap. Line four is the same as the second row. So there is a result of false false true.

Then verify with a piece of code:

 Public Static voidMain (string[] args) {String str1=NewString ("ABC"); String str2="ABC"; String STR3=NewString ("ABC"); System. out. println (str1 = =Str2.intern ()); System. out. println (str1 = =Str3.intern ()); System. out. println (str2 = =Str3.intern ()); System. out. println (str2 = =Str1.intern ());}

Result: false True True

First Intern () method:

public string Intern () returns the normalized representation of a string object. (I'm not sure what that means.)
When the Intern method is called, if the pool already contains a string equal to this string object (which is determined by the Equals (object) method), the string in the pool is returned. Otherwise, this string object is added to the pool, and a reference to this string object is returned. It follows for any two strings s and T, and s.intern () = = T.intern () is true only if and only if S.equals (t) is true.

That is, the string object returned by the Intern () method is definitely the object in the pool and the contents of the string are the same as the contents of the object that called the method. It is not difficult to understand that the result is false true. The object returned by Str3.intern () and Str1.intern () is the object that str2 points to. So our conclusions are also verified.

In addition to the case of string concatenation:

 Public Static voidMain (string[] args) {String str1="ABCD"; String str2="AB"; String STR3="CD"; String STR4= str2 +STR3; String STR5="AB"+"CD"; System. out. println (str1 = =STR4); System. out. println (str1 = =STR5);}

Result: false True

Then it may be confusing again, haha. This is where the program is interesting.

First of all, the first result is false. Should the object pointed to by STR4 not be an object in the buffer pool? The truth should be returned true. This involves the mechanism of string concatenation. Originally two strings str1, str2 splicing first calls string.valueof (obj), this obj is str1, and the implementation in string.valueof (obj) is return obj = = null? "Null": Obj.tostring (), then generates StringBuilder, calls the StringBuilder (STR1) Construction method, initializes the StringBuilder, and the length is str1.length () +16. The StringBuilder object is created on the heap at this time! , then call Stringbuilder.append (STR2), stitch the second string in, and call stringbuilder.tostring to return the result. So it returns false.

For this second result, the JVM will directly consider "AB" + "CD" as "ABCD". In fact, the JVM's handling of this time's + (plus) is completed at the compile date. At this time it did not involve StringBuilder.

  

Java create string object mechanism string buffer pool string concatenation mechanism

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.