Why Java String designed to be immutable?

Original from: Why IS immutable String in the Java?

It is derived from Java and Android interviews often asked of a topic. In the Java language, String is a string constant, StringBuilder is a string variable, why would String designed to be immutable (immutable)?

All information is initialized when Java, the String class is immutable, immutable class is an instance of a class can not be modified, the instance is created, and can not be modified. This design has many advantages, this article from memory, synchronization, and data structure summarizes the reasons why the String class designed to be immutable.

1, expects a string constant pool

String constant pool is a special storage area. When you create a string, if the string already exists in the pool, then returns a reference to an existing string, but does not create a new object.
The following code only create a string object in the heap:

String string1 = "abcd";
String string2 = "abcd";

Diagram is as follows:
Write pictures described here
If the String is variable, with a reference value change will result in an error string other references.

2, HashCode cache

A string stored in Java typically use HashCode value, e.g., in a HashMap or HashSet. Static can only guarantee HashCode, do not consider the change in him. This means that no every time you use it when they are calculated HashCode, improve operational efficiency.
String codes in the following class:

private int hash;//this is used to cache hash code.

3, to promote the use of other objects

The following program we specify:

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";

In this example, if the string is variable, he would be contrary to the design set (the set of elements comprises a non-repeating). Of course, the above examples are merely used for demonstration, there is no real value field string class.

4, security

String is widely used in many Java class parameters, such as network connections, open files and the like. If the string is not immutable, connection or file changes will lead to a serious security threat. He believes that is connected to a machine, it is not. Because the argument is a string, the string variable can lead to security problems in reflection. (Immutability so that it can be shared among different threads, and thread-safe, to help users reduce development thread synchronization)
example 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 naturally thread-safe

Because immutable objects can not be changed, so they can share multiple threads freedom. This eliminates the requirement that they be synchronized.

to sum up

In short, String is designed to be immutable factors for efficiency and safety. This is why in many cases the preferred class is the cause of immutable class.

Of course, different usage scenarios reflect the advantage immutable characteristic is not the same. We can recall their own projects use scenario, think more such problems.

Published 59 original articles · won praise 88 · views 190 000 +

Guess you like

Origin blog.csdn.net/geofferysun/article/details/78104676