In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail what the JavaString object is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Java String object, do you really understand it?
String object is one of the most frequently used objects in Java, so Java is constantly optimizing the implementation of String object in order to improve the performance of String object. Take a look at the figure below to understand the optimization process of String object.
In Java6 and previous versions, the String object is an object that encapsulates the char array and has four main member variables: char array, offset offset, number of characters count, and hash value hash. The String object locates the char [] array and gets the string through two properties, offset and count. Doing so allows you to share array objects efficiently and quickly while saving memory space, but this approach is likely to lead to memory leaks. two。 From the Java7 version to the Java8 version, starting with the Java7 version, Java has made some changes to the String class. There are no more offset and count variables in the String class. The advantage is that the String object takes up a little less memory, and the String.substring method no longer shares char [], thus solving the memory leak problem that may be caused by using this method. 3. Why is it necessary to change the char [] array to the byte [] array since the Java9 version? We know that char is two bytes, and if it is a bit wasteful to store one byte of characters, Java changed it to an one-byte byte to store strings in order to save space. In this way, waste is avoided by storing one byte of characters. A new property, coder, is maintained in Java9, which is the identifier of the encoding format. When calculating the string length or calling the indexOf () function, you need to use this field to determine how to calculate the string length. The coder attribute defaults to values 0 and 1, where 0 represents Latin-1 (single byte encoding) and 1 represents UTF-16 encoding. If String determines that the string contains only Latin-1, the value of the coder property is 0, and vice versa.
In the form of String str= "pingtouge" in the form of string constants, when creating a string in this form, JVM first checks whether the object exists in the string constant pool, returns the reference address of the object, if not, creates the string object in the string constant pool and returns the reference. The advantage of creating in this way is that it avoids repeated creation of strings with the same value and saves memory. 2. The String () constructor is in the form of String str = new String ("pingtouge"). The process of creating a string object in this way is more complicated, which is divided into two phases. First, the string pingtouge will be added to the constant structure at compile time, and the string will be created in the constant pool when the class loads. Then, when new () is called, JVM will call the constructor of String, reference the pingtouge string in the constant pool, create a String object in heap memory, and return the reference address in the heap. Now that we understand the two ways to create String objects, let's analyze the following code to deepen our understanding of these two ways. Is str equal to str1 in the following code chip?
Let's analyze these lines of code one by one, starting with String str = "pingtouge". Here we use string constants to create a string object. When creating a pingtouge string object, JVM will go to the constant pool to find out whether the string exists. The answer here is definitely no, so JVM will create the string object in the constant pool and return the address reference of the object. So str points to the address reference of the pingtouge string object in the constant pool.
Then there is the line of String str1 = new String ("pingtouge"), where the constructor is used to create the string object. According to our above understanding of how the constructor creates the string object, str1 should get the reference address of the pingtouge string in the heap. Since str points to the address reference of the pingtouge string object in the constant pool and str1 points to the reference address of the pingtouge string in the heap, str is definitely not equal to str1.
From the moment we knew about the String object, I think everyone knew that the String object was immutable. So how does it remain immutable? what are the benefits of Java doing this? Let's have a brief discussion. Let's take a look at a piece of source code for the String object:
As can be seen from this source code, the String class uses the final modifier. We know that when a class is modified by final, it indicates that the class cannot be inherited, so the String class cannot be inherited. This is the first point that String is immutable.
Further down, the char value [] array used to store strings is modified by private and final, and we know that for a variable of the basic data type of final, its value cannot be changed once initialized. This is the second immutable point of String. Why Java set String to immutable, mainly from the following three aspects: 1, to ensure the security of String objects. Assuming that the String object is mutable, the String object may be maliciously modified. 2. Ensure that the hash attribute value will not change frequently, ensuring uniqueness, so that similar HashMap containers can achieve the corresponding key-value cache function. 3. String constant pool can be realized.
About what the JavaString object is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.