1. Variables defined by the string class are not allowed to become
Eg:String s0= "Kvill";
string S1 = new String ("Kvill");
Why design to be immutable:
1.1 The need for a string constant pool
1.2 allows string object caching Hashcode
Hash codes for string objects in Java are used frequently, such as in containers such as HashMap.
String invariance guarantees the uniqueness of the hash code, so you can cache it with confidence. This is also a performance optimization tool, meaning that you do not have to calculate a new hash code every time. The following code is in the definition of the string class:
private int hash; / /used to cache hashcode
1.3 security
String is used by many Java classes (libraries) as parameters, such as the network connection address URL, file path, and the reflection mechanism required by the string parameter, if the string is not fixed, it will cause a variety of security risks.
2,String.intern ()
a method of extending a constant pool at run time.
When a string instance str calls the Intern () method, Java looks for a constant pool with the same Unicode string constant, if any, returns its reference, if not, adds a string of Unicode equal to STR in the constant pool and returns its reference
String class in Java