Java create string object mechanism string buffer pool string concatenation mechanism string intern () method

Source: Internet
Author: User
Tags true true

string constant pool: string constant pool in method area

To optimize space, the string class maintains a string pool to reduce the number of strings created in the JVM , and the JVM checks the string constant pool first whenever the code creates a string constant. If the string already exists in the pool, the instance reference in the pool is returned. If the string is not in the pool, a string is instantiated and placed in the pool. Java can be optimized because the strings are immutable and can be shared without worrying about data conflicts . Therefore, these strings in the constant pool are not recycled by the garbage collector

1.String str = new String ("Hello"); the 2 objects are created at this point, but the reference STR only points to the objects in the heap and does not point to the objects in the constant pool

2 objects were created, 1. Check that there is no hello in the constant pool, no, create the object in the constant pool, and then create the object to be placed in the heap. If the constant pool has a Hello object, only one object is created and placed in the heap.

2. String constant pool in method area

3.String str = "Hello"; At this point, 1 objects are created, referencing Str to the objects in the constant pool

Check that the constant pool has no hello, and if so, point to the object, and if not, create the object in the constant pool.

4.intern () method. To turn a string into a string in a constant pool, that is: to point a reference to an object in a constant pool

If the constant pool already contains a string equal to that string object, the string is returned. Otherwise, the string object is added to the constant pool and the reference is returned.

5.String S1 = "Tom";

String s2 = "Cat";

String s3 = "Tomcat";//Creates an object in a constant pool.

String S4 = "Tom" + "cat";//Do not create objects, theJVM considers string concatenation with double quotes as a whole, and Tomcat already exists in the constant pool, so no objects are created

String S5 = S1 + s2;//creates an object "tomcat"

S3 = = S4;true

S3 = = S5;false

Why is it false?

The concatenation of string references is not equal to the concatenation of strings with double quotation marks , the original two strings S1, S2 splicing first calls string.valueof (obj), this obj is S1, and string.valueof (obj) is the implementation in the return obj = = null? "Null": Obj.tostring (), then generates StringBuilder, calls the StringBuilder (S1) 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.

Called String.valueof (), produced a StringBuilder, called its constructor, new object on the heap, and then used the append () method to stitch the second string S2 together.

Instance:

public static void Main (string[] args) {    string str1 = new String ("abc");    String str2 = "abc";    String str3 = new String ("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.

Java create string object mechanism string buffer pool string concatenation mechanism string intern () method

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.