Why are strings immutable in Java? __string

Source: Internet
Author: User

This is an old but still popular problem. The strings in Java are designed to be immutable for a number of reasons. The good answer depends on your memory, synchronization, data structure and so on better understanding, below, I summarize some answers.

Requirements in the 1.String pool

A string pool is a special type of storage area in the method region. When a string is created, if the string already exists in the pool, the reference to the existing string is returned instead of creating a new object and returning its reference.

The following code will create only one string object in the heap.

String string1 = "ABCD";
String string2 = "ABCD";

This is the case in the heap:


If the string is not immutable, the ever-changing string under only one reference causes the wrong value to be obtained for other references.

2. Allow strings to cache their hash codes

In Java, the hash code of a string is often used. In HashMap, for example, immutable guarantees that the hash code will always be the same, so there is no need to worry about the change of the string to implement this immutable feature, which means that each time it is used it is not necessary to compute its hash code, so this is more efficient.

In the string class, the following code is in the class:

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

3. Safety
Strings are widely used as parameters for many Java classes, such as network connections, opening files, and so on, if the strings are mutable, the connection, opening the file will be changed and can cause serious security threats. The method is also thought to be connected to a machine, but that is not the case. Variable strings can also cause security problems in the reflection mechanism because these parameters are strings.

The following is a code example:

Boolean connect (string s) {
    if (!issecure (s)) { 
throw new SecurityException (); 
}
    Here would cause problem, if it changed before this by using the other references.    
    Causeproblem (s);
}

To sum up, reasons include design, efficiency and security. In fact, this also applies to many other "why" questions in a Java interview.


Original: Why string is immutable in Java?


Related Article

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.