Third, why string in Java is non-changing

Source: Internet
Author: User
Tags object object

String is a class that cannot be changed in Java. A class that cannot be changed simply means that all instances of this class cannot be changed. All instance information is initialized at the time of creation and the information is not changed. Classes that cannot be changed have many benefits.
This article summarizes why a string is designed to be immutable. A good answer requires an in-depth understanding of memory, synchronization, and data structures.
1, the need of string pool
A string pool (a string inner pool) is a special area in a method area. When a string is created, if the string already exists in memory, the reference to the existing string is returned instead of creating a new object and returning a reference to it.
The following code creates a string object on the heap.

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




If the string is to be changed, changing a string by a reference causes another reference to point to the wrong value.
2. Cache hash Value
In Java, the hash value of a string is often used. For example, in a hashmap. Remains constant, ensuring that the same hash value is always returned. So it can be cached without worrying about being changed. This means that this does not require a hash value to be computed every time it is used.
This will be more efficient.
In the string class, it has the following code:

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


3, make the use of other classes easier.
To be more specific, think about the following procedure:

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 case, if the string can be changed, if its value is changed this will violate the design of set (set cannot contain duplicate elements). This example is intended to simplify the design, and there is no value attribute in the actual string class.

4. Security
String in many Java classes, in Network Connections, open files are often used as parameters. If a string can be changed, a connection or file will potentially be changed, which will lead to serious security threats. This method thinks that it is connecting to a machine, but it is not actually. The variable strings will cause a security problem in reflection or as a parameter.
Here is the code example:

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 natural thread-safe
Because immutable objects cannot be changed, it can be shared freely by multiple threads. This eliminates the synchronization.
In summary, string is designed to be not changed for efficiency and security. That's why there are so many classes that can't be changed now.

Third, why string in Java is non-changing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.