Today, my friend asked me if the String content is really immutable? I must have told him yes? In my subjective consciousness, String is an immutable object. So he sent me this program:
main(String[] args) = "chenssy""a = " += String..getDeclaredField("value".setAccessible([] value=(.get(a); value[4]='_'; System.out.println("a = " +
I smiled when I saw this simple program. Didn't you modify the String value from the underlying layer? Here, we can understand that the value of String must be changeable (we should always believe that the value of String cannot be changed )! Then he gave me another program:
Main (String [] args) = "chenssy" = "chenssy" = String ("chenssy" "-------------- modify the previous value -------------------" "a =" + "B =" + "c =" + Field a _ = string .. getDeclaredField ("value" [] value = (4] = '_'; "-------------- modified value -------------------" "a =" + "B =" + "chenssy" "c =" +
At first glance, this program is very simple. It is nothing more than value assignment, value change, and output! Maybe you will not hesitate to say it is too simple now. The result is ....... But !! Without hesitation, you will be killed, and your results may be wrong. So what is the running result?
-------------- Modify the pre-value ------------------- ==------------ modify the post-value --------------------- === chen_ssy
The pre-modification value is easy to understand, but what about the modified value? Isn't that understandable? You may ask: why is System. out. println ("chenssy"); the result is chen_ssy, System. out. println ("c =" + c); is it chen_ssy?
You must understand that this is actually relatively simple. master a knowledge point: String constant pool.
We know that the allocation of strings is the same as that of other objects, which consumes a lot of time and space, and we use a lot of strings. To improve performance and reduce memory overhead, JVM has made some optimizations when instantiating strings: using the String constant pool. Whenever we create a String constant, JVM will first check the String constant pool. If the string already exists in the constant pool, it will directly return the instance reference in the constant pool. If the string does not exist in the constant pool, it is instantiated and put into the constant pool. Because of the immutability of the String, we are sure that there must be no two identical strings in the constant pool (this is important to understand the above ).
Let's take a look at the above program.
String a = "chenssy ";
String B = "chenssy ";
Both a, B, and the chenssy on the word surface point to the "chenssy" object in the JVM String constant pool. They point to the same object.
String c = new String ("chenssy ");
The new keyword will certainly generate a chenssy object (note that this chenssy is different from the preceding chenssy), and the object is stored in the heap. Therefore, two objects should be generated: c in the stack and chenssy in the stack. However, there are no exactly the same string objects in Java. Therefore, the chenssy In the heap should reference the chenssy In the String constant pool. Therefore, the relationship between c, chenssy, and pool chenssy should be: c ---> chenssy ---> pool chenssy. The entire Link is as follows:
= A + String ("ssy ");How many String objects are created ??