Why doesn’t String change?

1, what is immutable?

In Java terms, that’s final.
String is not variable as shown below:
hypothesis to string s value for the ABCD, second of the re set to ABCDEF, this time not to modify the data in the original memory address, but to redirect to a new object.

Why doesn't String change?
Paste_Image.png

2, String is not variable hair?

Look at the first 3 lines of the source code, you will understand.

Public final class String implements java.io.Serializable, Comparable<, CharSequence; String> The value is used for {/ * * * character storage. Private Final char value[];

1, first class String is modified with final, indicating that String may not inherit the
2, then the main member field value String class is a char[] array, and is modified with final, final field of modified after creation is not variable.

Note: Although value is indeed final, immutable, but only value, this reference address is immutable, can not stop Array array is variable fact.

Why doesn't String change?
Paste_Image.png

That is a reference to a stack Array variable is just in the pile body structure, array, modified with final value properties of the String class, just say stack in this value address can not be changed, not heap array itself data variable, for example:

Final int[] value = {1, 3}; int[] Value2 = 2, {11, 22, 33}; value = Value2; / / compiler error, modified with final value, to address the immutable

But as follows, yes

Final int[] value = {1, 2, 3}; value[0] = 11; / / this array has become {11, 2, 3}

Is that clear now?
String is the key variable is not a final may not be inherited, but the key lies in the underlying implementation, the modifier private effect of value was less than final, plus is final to prevent the inheritance, to avoid damage.

3, what is the advantage of non change?

Two words: Security

Public static / / immutable String String appendStr (String STR) {STR = "ABC"; return str; static StringBuilder appendSb} public (StringBuilder sb) {return sb.append ("def");} public static void main (String[] args) {/ / String = new String S1 String parameter ("ABC"); String S2 = Demo2.appendStr (S1); System.out.println ("-") + s1.toString (//StringBuilder); StringBuilder sb = new StringBuilder parameter ("ABC"); StringBuilder SB2 = Demo2.appendSb (sb); System.out.println ("-") (+ sb.toString);}

Result:

-------------abc -------------abcdef

It can be found that the value of String has not changed, and that is where he is safe.

Download the example again:

Public static void main (String[] args) {HashSet< = new; StringBuilder> HS; StringBuilder> HashSet< SB1 = new (StringBuilder) StringBuilder ("ABC"); StringBuilder SB2 = new StringBuilder ("abc123"); hs.add (SB1); hs.add (SB2); / / this is HashSet {"ABC" "abc123"}, System.out.println (hs.toString) (StringBuilder); Sb3 = SB1; sb3.append ("123"); / / this is HashSet {"abc123", "abc123"} (hs.toString) (System.out.println);}

Result:

[abc, abc123], [abc123, abc123]

The StringBuilder variables SB1 and SB2 point to the literal “ABC” and “def” of the heap memory, and insert them into a HashSet, and there’s nothing wrong with that. But later I also point the variable Sb3 to the address of SB1, changing the value of Sb3 because StringBuilder has no immutable protection. Sb3 changes directly to the original “ABC” address, resulting in a change in the value of sb1. At this point, two equal key values appear on HashSet, “abc123″”. The uniqueness of the HashSet key is destroyed, so never make HashMap and HashSet keys with mutable types

Also, there are multiple threads that read one resource at the same time, and do not cause unexpected conditions. Only when writing to resources is dangerous, immutable objects cannot be written, so thread safety.

Finally, don’t forget that String comes with the properties of the constant pool, as follows

Why doesn't String change?
Paste_Image.png

There will be only one space in heap memory. Save memory space and improve efficiency.

If interested, welcome to join the group, [Java beginners learning exchange group]: 458430385, this group has Java developers, UI designers and front-end engineers. Answer, study together, progress together!