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

Why use String

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you why to use String, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Recently, I have been mentoring beginners during the training course. One of the tasks is to complete a class that requires the class to perform a dwarwle operation on a map whose key is of type String. In the class completed by one of the students, there are the following methods:

Void dwarwle (HashMap mapToDwarwle, String dwarwleKey) {for (final Entry entry: mapToDwarwle.entrySet ()) {dwarwle (entry.getKey (), entry.getValue (), dwarwleKey);}}

This code is generally OK. This method passes the key and value of each Dwarable in the map, along with the dwarwleKey it expects to be decomposed, to another calling method. Because the function is simple, I will not describe it in detail. As long as you understand the meaning of dwarwle, you can easily know what this method will do. Such functions are simple and have good readability. However, this method expects the parameter to be a HashMap, not a Map. Why are we forcing callers to use HashMap here? If the caller needs to use TreeMap for some reason, do you want to add another same method to accept TreeMap? Of course not.

"the parameter type uses an interface, and the object that implements the interface is passed in when called."

The beginner uses Map instead of HashMap. But about five minutes later, the clever lady asked another question:

"if we replace HashMap with Map, why not replace String with CharSequence?"

It's not easy to answer such a question all of a sudden. First of all, I think that's what we usually do, and that's why. But this answer is not persuasive at all, at least I will not accept such an answer, and I hope my students will not accept such an answer. This is a very authoritarian answer.

The real answer is that because this parameter is used as the key of Map, the key of Map is usually expected to be immutable (at least the change does not affect the calculation of equals and hashCode). CharSequence is an interface, and Java does not specify the variability of the interface, which can only be determined by the specific implementation. String is a concrete implementation of CharSequence, widely known and rigorously tested, so it is a good choice here.

In this specific example, we prefer String because it is Immutable. And we cannot fully trust that the caller will pass an immutable CharSequence implementation. If we can trust the caller, then we may pay the price. When StringBuilder is passed to the method as an argument, and its value has changed since then, the class library we wrote will probably not work. When designing an API or class library, we need to consider not only some of the possibilities we expect, but also the possibilities in reality.

"practice is the criterion for testing truth."

This is not limited to class libraries, but may also apply to other products. This seems to have gone too far.

The above is why you use String. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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