As a great god you know why Java is designing strings to be immutable!

Source: Internet
Author: User

String is an immutable class in Java that cannot be modified once instantiated. Once an instance of an immutable class is created, the value of its member variable cannot be modified, and the immutable class has many advantages. Everyone knows why Java designers want to design it as an immutable class, and let's talk about it.


String pool

A string pool is a special store that is part of a method area. When a string is created, it is first searched in the string pool, and if found, returns a reference to the string directly.

The following code will only create a string in the heap

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


Here is the illustration:



If the string is variable, when the two references point to the same string, modifying one of the two will affect the other. (Remember the impact and help you understand what's behind it)


Cache hashcode

The hash code (hashcode) of a string is often used in Java. For example, in HashMap, the immutable character of a string guarantees that its hashcode will always be consistent, thus avoiding unnecessary hassles. This means that it is more efficient to use the hashcode of one string at a time without having to recalculate it again.

In the string class, the following code is available:

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


The hash variable in the above code holds the hashcode of a string object because the string class is immutable, so the hash value cannot be changed once the object is created. So, every time you want to use the object's hashcode, you can return directly.


make the use of other classes more convenient

Before you introduce this content, look at the following code:

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 the above example, if the string can be changed, then the above usage will likely violate the set design principle, because set requires that the elements in it cannot be duplicated. The code above simply explains the problem, but there is no value for this field in the String class.


Security


String is widely used as a parameter in other Java classes. such as network connections, open files and other operations. If the string is variable, a similar operation can cause a security issue. Because a method calls a connection operation, he thinks it will connect to a machine, but it does not (the other value modification that references the same string object causes the string content in the connection to be modified). A mutable string can also cause a security problem with reflection because his argument is also a string.

code example:

Boolean connect (string s) {    if (!issecure (s)) {throw new SecurityException ();}    If S is changed by another reference before the operation, it can cause problems.       Causeproblem (s);}


immutable objects are inherently thread-safe

Because immutable objects cannot be changed, they are free to share between multiple threads. No synchronous processing is required.

In summary, the main purpose of the string being designed as immutable is to be safe and efficient. So, making string is an immutable class is a good design.


Reproduced in: http://www.importnew.com/18326.html

As a great god you know why Java is designing strings to be immutable!

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.