Java String Learning

Source: Internet
Author: User

String,
First, string has the concept of a literal literal constant , which is determined at compile time, and is stored directly in Chang when the class is loaded (note that a constant pool is a constant pool of classes , isolated between classes and classes).
The string that is generated at run time is not in the constant pool. If you want to put in a constant pool, you can use the intern () method.

Second, like a literal string, because it is stored in a constant pool and referenced by a constant pool, it cannot be GC.

Other knowledge:

In general, strings are immutable (imutable), so thread-safe.

However, you can modify the string by means of reflection !!! Because the underlying is char[], it can be forcibly modified after getting it.

/*** Modify string!!! * @throwsnosuchfieldexception *@throwsillegalaccessexception*/@Test Public voidRun6 ()throwsnosuchfieldexception, illegalaccessexception {String S1= "ABCDEFG"; System.out.println ("Pre-modification: s1=" +S1); Field value= String.class. Getdeclaredfield ("value");    //value is a string internal field:private final char value[];   Value.setaccessible (true); Char[] cs = (Char[]) value.get (S1); //cs = "ent". ToCharArray ();//This is a reference change, but the inside of the string is unchanged (private final char value[];)Cs[0] = ' A '; System.out.println ("Modified: s1=" +s1);}

It is important to note that you cannot modify the reference, but modify the array element directly!!!

Java String Learning

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.