we use the following photos to illustrate the invariance of Java.
1. Declaring a string
String s = "abcd";
s stores a reference to a string object. The arrows in the picture below represent this kind of storage reference.
2. Assigning a string variable to another string variable
String s2 = s;
The S2 variable stores the same reference value. So, two variables point to the same string object.
3. Merging strings
s = s.concat("ef");
s now stores a reference to the newly generated string object.
4. Summary
Once a string is created on the memory (heap), the string does not change. We should note that all methods of the string class do not change the string itself, but instead return a new string.
If we need a string that can be changed, we can use StringBuffer or StringBuilder. Otherwise, because each time a new string is created.
[Java Development Road] (8) The invariance of the Illustrated string