Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the reason why String is immutable in Java?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what is the reason why String is immutable in Java", the content is simple and clear, hoping to help you solve your doubts, the following let the editor lead you to study and learn "what is the reason why String is immutable in Java"?

In Java, the String class is immutable. To put it simply, an immutable class means that its instance is immutable, and all the information of the instance is initialized and unmodifiable when the instance is created. The design of immutable classes has many advantages. This blog post mainly illustrates this immutable concept from the perspective of memory, synchronization and data structures.

The need for String Pool String Pool (String intern pool) is a special storage area in the method area. When a String is created, if it is found that the current String already exists in the String Pool, a reference to the existing String is returned instead of creating a new object.

The following code creates only one String object in heap memory.

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

The following figure shows the process of creation:

If a String is mutable, changing the String that one reference points to will cause other references to get the wrong value.

Caching Hashcode in Java, Hashcode for String is used very frequently, such as in HashMap or HashSet. Designing String to be immutable ensures that his Hashcode is always consistent so that Hashcode can be cached without having to worry about changes. This means that you don't have to calculate String's Hashcode every time you use it, which makes the program run more efficiently.

In the String class, the code for Hashcode is as follows

Private int hash;//this is used to cache hash code. To simplify the use of other objects, for more detail, let's consider the following procedures:

HashSet set = new HashSet (); 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 String is mutable, it violates set's design intent (set contains non-repeating elements). Of course, the above example is just to demonstrate that there is no value field in the String class.

Secure String is widely used as a parameter in many Java classes, such as network connection, file opening, and so on. Assuming that the String is mutable, a connection or a file may be changed, which can lead to serious security risks. Some method thought it was connecting to a machine, but it didn't. Mutable String can also cause security problems when reflecting, because the parameter type of reflection is also String.

The following is a code example:

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);} immutable objects are inherently thread safe because immutable objects cannot be changed, they can be freely shared in multiple threads, which eliminates the need for object synchronization.

In general, String is designed to be immutable from the starting point of efficiency and security. This is why immutable classes are preferred in many cases.

The above is all the content of the article "Why is String immutable in Java?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report