In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use String commands in Java development". In daily operation, I believe many people have doubts about how to use String commands in Java development. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use String commands in Java development". Next, please follow the editor to study!
String s = "Hello"; s = s + "world!" in Java development; has the content in the original String object changed after the execution of these two lines of code? No, because String is designed to be an immutable class, all its objects are immutable. In this code, s originally points to a String object with the content "Hello", and then we do a + operation on s.
So has the object that s points to changed? The answer is no. At this point, s no longer points to the original object, but points to another String object with "Helloworld!" inside, which still exists in memory, but the reference variable s no longer points to it. From the above explanation, it is easy to draw another conclusion that using String to represent a string can incur a lot of memory overhead if you often make various changes to the string, or unforeseen changes.
Because the String object cannot be changed after it is created, a String object is needed to represent each different string. At this point, you should consider making the StringBuffer class, which allows modification, rather than generating a new object for each different string. Moreover, the conversion between these two kinds of objects is very easy.
At the same time, we also know that if you want to use a string with the same content, you don't have to new one String at a time. For example, to initialize a String reference variable called s in the constructor and set it to the initial value, you should do this:
The latter calls the constructor every time to generate a new object, which is slow in performance, high in memory overhead, and meaningless, because the String object is immutable, so for a string with the same content, it only needs a String object to represent it. That is to say, the above constructor is called multiple times to create multiple objects, and their String type properties s all point to the same object. The above conclusion is also based on the fact that for string constants, Java believes that they represent the same String object if the contents are the same.
Calling the constructor with the keyword new always creates a new object, regardless of whether the content is the same or not. Why the String class is designed to be immutable is determined by its purpose.
In fact, not only String, the classes in many Java standard class libraries are immutable. When developing a system, we sometimes need to design immutable classes to pass a set of related values, which is also the embodiment of object-oriented thinking. Immutable classes have some advantages, for example, because its objects are read-only, there is no problem with multithreaded concurrent access. Of course, there are some disadvantages, such as each different state is represented by an object, which may cause performance problems. So the Java standard class library also provides a variable version, StringBuffer.
At this point, the study on "how to use String commands in Java development" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.