StringBuffer Intern method After turning string

Source: Internet
Author: User

In the process of generating strings with stringbuffer, different stitching methods affect the position of the final string object.

Case one: Declaring an object with a directly defined string

 Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString str1=NewStringBuffer ("Computer"). Append ("Software")). toString (); String str2=NewStringBuffer ("Computer Software"). toString (); String STR3=NewStringBuffer ("Computer Software"). Append (""). toString (); String STR4=NewStringBuffer (). Append ("calculation"). Append ("Machine Software")). toString (); String STR5=NewStringBuffer (). Append ("Computer"). Append ("Software").). toString (); String STR6= "Computer Software"; System.out.println ("Str1.intern () ==str6\t" + (str1.intern () = =STR6)); System.out.println ("Str2.intern () ==str6\t" + (str2.intern () = =STR6)); System.out.println ("Str3.intern () ==str6\t" + (str3.intern () = =STR6)); System.out.println ("Str4.intern () ==str6\t" + (str4.intern () = =STR6)); System.out.println ("Str5.intern () ==str6\t" + (str5.intern () = =STR6)); System.out.println ("Str6.intern () ==str6\t" + (str6.intern () = =STR6)); }}

Output Result:

Str1.intern () ==STR6 True
Str2.intern () ==str6true
Str3.intern () ==str6true
Str4.intern () ==str6true
Str5.intern () ==str6true
Str6.intern () ==str6true

Analysis: When the string has a direct string constant "computer software", and assigned to STR6, so STR6 directly refers to the constant pool object, namely Str6.intern () =STR6, the other string intern method also corresponds to the STR6 object in the constant pool.

Case two: The Declaration object does not contain a directly defined string, but is generated by the StringBuffer object more than two times by the Append method

        String str7=New StringBuffer (). Append ("calculation"). Append ("Machine Network"). toString ();        String str8=new stringbuffer (). Append ("Computer"). Append ("Network"). toString ();        String STR9=new stringbuffer ("Count"). Append ("Computer network"). toString ();        System.out.println ("Str7.intern () ==str7" + (str7.intern () = =str7));        System.out.println ("Str8.intern () ==str7" + (str8.intern () = =str7));        System.out.println ("Str9.intern () ==str7" + (Str9.intern () ==str7));

Output Result:

Str7.intern () ==str7 True
Str8.intern () ==str7 True
Str9.intern () ==str7 True

Parsing: When there is no complete final string ("Computer network") in the string, and each string is stitched together by two (and more) small strings, the first generated string is the same as the string in the constant pool, and whoever generates it assigns the string in the constant pool to who (here STR7). If the above string. Intern () and str8 or STR9 are compared, the result is false.

Case three: The Declaration object does not contain a directly defined string, but it has a string that is generated through the StringBuffer object at a time.

String str1=NewStringBuffer ("Computer"). Append ("Software")). toString (); String str2=NewStringBuffer ("Computer Software"). toString ();//String str3=new stringbuffer ("Computer Software"). Append (""). toString ();String str4=NewStringBuffer (). Append ("calculation"). Append ("Machine Software")). toString (); String STR5=NewStringBuffer (). Append ("Computer"). Append ("Software").). toString (); System.out.println ("Str1.intern () ==str2\t" + (str1.intern () = =str2)); System.out.println ("Str2.intern () ==str2\t" + (str2.intern () = =str2));//System.out.println ("Str3.intern () ==str3\t" + (Str3.intern () ==str3));System.out.println ("Str4.intern () ==str2\t" + (str4.intern () = =str2)); System.out.println ("Str5.intern () ==str2\t" + (Str5.intern () ==str2));

Output Result:

Str1.intern () ==str2 false
Str2.intern () ==str2false
Str4.intern () ==str2false
Str5.intern () ==str2false

Parsing: String constants are not paid to any string object when there is a string object generated once through StringBuffer in the strings. Similarly, if the string str2 is unregistered and replaced with STR3, the result is still false.

Summarize:

    • A string object directly corresponds to a constant pool object when it is generated directly from literal values.
    • If each string is stitched through a StringBuffer object with two or more small strings, and there are no string objects that are generated entirely from literal values, the first generated string corresponds to the string object in the constant pool.
    • If the generated string object has a string object that is generated by a complete string or a full string of concatenation of empty strings by a StringBuffer object and does not have all literal values generated, then all string objects do not correspond to objects in the constant pool

StringBuffer Intern method After turning string

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.