Original address: # Why String was immutable in Java?
As we all know, the string class is immutable in Java. Immutable classes are simply instances of classes that cannot be modified. When an instance is created, all information cannot be modified when it is initialized. Immutable classes have many benefits, and this article briefly describes why the string class is designed to be immutable. This article will explain the concept of invariance from the angle of memory, synchronization and data structure.
1. The need for a string constant pool
A string constant pool is a special storage area for the method area. When a new string is created, if the string already exists in the constant pool, it returns a reference to a string that already exists, rather than creating a new object.
The following code shows that a string object is created only in heap memory (the string constant pool is in heap memory).
String string1 = "abcd";String string2 = "abcd";
Here is a vivid explanation for the following picture:
Finally, imagine that if a string is mutable, then using a reference to change the value of the string will cause other references to point to the wrong value.
2. Cache Hashcode
Strings are hashcode
frequently used in Java, such as in HashMap or HashSet. The same is always the same as the guarantee of the hashcode
string, so you can operate without worrying about changes. This means that it is more efficient to calculate it every time you use it hashcode
.
In the string class, you have the following code:
private int hash;//this is used to cache hash code.
3. Make other objects easier to use
To explain in detail, look at the code below:
HashSet<String> set = new HashSet<String>();set.add(new String("a"));set.add(new String("b"));set.add(new String("c")); for(String a: set) a.value = "a";
Imagine that if a string is mutable (that is, it is added to change the value of the string), then the Set
rules of the set (elements cannot be duplicated) will be violated.
Of course, the above code is just a demonstration, and there is no Value property in the String class.
4. Security
The string class is widely used in many classes of Java (as a parameter to a method), such as a network connection, opening a file, and so on.
If the string class is mutable, a connection or file can be changed, which can lead to serious security threats. In reflection, an unstable string can also pose a security problem.
The code is as follows:
boolean connect(string s){ if (!isSecure(s)) { throw new SecurityException(); } //here will cause problem, if s is changed before this by using other references. causeProblem(s);}
5. Immutable objects are thread-safe
Because immutable objects cannot be changed, they can be shared freely in multiple threads. This also eliminates the need for synchronization.
In general, the string is designed to be immutable primarily in terms of efficiency and safety. These two points are also the reason why immutable classes are more popular in many cases.
Why is the Java string class mutable?