Re-extract string from Java

Source: Internet
Author: User

Original from: http://cmsblogs.com/?p=863. Respect the results of the author, reproduced please specify the source!

Personal website: http://cmsblogs.com

----------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------

Today, my friend asked me if the contents of string are really immutable. I'm sure I told him, right? Because in my subjective sense, string is an immutable object. So he sent me this procedure:

public class Stringtest {public    static void Main (string[] args) throws Exception {        String a = "Chenssy";        System.out.println ("a =" + a);        Field A_ = String.class.getDeclaredField ("value");        A_.setaccessible (true);        Char[] value= (char[]) a_.get (a);        Value[4]= ' _ ';   Change the value pointed to by a        System.out.println ("a =" + a);}    }

See this simple program, I laughed, you this is not from the bottom to change the value of string? From here we understand that the value of string must be able to change ( we should always believe in the immutability of String)! Then he gave me another procedure:

public class Stringtest {public    static void Main (string[] args) throws Exception {        String a = "Chenssy";        String B = "Chenssy";        String c = new String ("Chenssy");        System.out.println ("--------------Pre-change Value-------------------");        System.out.println ("a =" + a);        System.out.println ("b =" + b);        System.out.println ("c =" + C);        Change the value of String        Field A_ = String.class.getDeclaredField ("value");        A_.setaccessible (true);        Char[] value= (char[]) a_.get (a);        Value[4]= ' _ ';   Change the value pointed to a                System.out.println ("--------------Modified Value-------------------");        System.out.println ("a =" + a);        System.out.println ("b =" + b);        System.out.println ("Chenssy");        System.out.println ("c =" + C);}    }

At first glance This program is unusual simple, is nothing more than assignment, change value, Output! Perhaps you will not hesitate to say it is too simple now, the result is .... But!! Your hesitation will kill you, and your results are likely to be wrong. So what is the result of the execution?

--------------changes before value-------------------a = Chenssyb = CHENSSYC = Chenssy--------------Modified value-------------------A = Chen_ SYB = CHEN_SYCHEN_SYC = Chen_ssy

The pre-change value is easy to understand, but what about the post-change value? Is it a little bit incomprehensible? You may ask: why System.out.println ("Chenssy"); The result would be chen_ssy,system.out.println ("c =" + C); and Chen_ssy?

to make this clear, it is actually simpler to master a knowledge point: a string constant pool.

We know that the allocation of strings, like other object assignments, requires a lot of time and space, and strings we use very much. In order to improve performance and reduce the cost of memory, the JVM has some optimizations when instantiating strings: using a string constant pool. Whenever we create a string constant, the JVM first checks the string constant pool, assuming that the string already exists in the constant pool, and returns the instance reference in the constant pool directly. Assuming that the string does not exist in a constant pool, the string is instantiated and placed in a constant pool. because of the immutability of string strings we can be quite certain that there must be no two identical strings in the constant pool (this is critical to understanding).

Let's try to understand the above procedure.

String a = "Chenssy";

String b = "Chenssy";

A, B, and literally Chenssy are "chenssy" objects that point to the JVM string constant pool, and they point to the same object.

String c = new String ("Chenssy");

Newkeyword is bound to produce an object Chenssy (note that this chenssy differs from the chenssy above) and that the object is stored in the heap at the same time. So the above should produce two objects: Keep the C in the stack and save the Chenssy in the heap. In Java, however, there is no two completely identical string objects. So the chenssy in the heap should be chenssy in the reference string constant pool. So c, Chenssy, pool chenssy relationship should be: c--->chenssy---> Pool chenssy. The entire relationship such as the following:


We can understand the relationship between them very clearly through the above figure. So we change the value in memory, and he changes all of it.

Summary: Although a, B, C, Chenssy are different objects, from the internal structure of string we can understand the above. String c = new String ("Chenssy"), although the content of C is created in the heap, his internal value is also a chenssy to the JVM constant pool, which is still chenssy string constant when it constructs chenssy.

in order to give you a full understanding of the constant pool, a simple topic such as the following is prepared specifically:

String a = "Chen"; String B = A + new String ("Ssy");

How many string objects are created??

--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------

Original from: http://cmsblogs.com/?p=863. Respect the results of the author, reproduced please specify the source!

Personal website: http://cmsblogs.com


Re-extract string from Java

Related Article

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.