In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the methods of using Java String objects". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "what are the methods of using Java String objects" together!
Implementation of String object
String object is one of the most frequently used objects in Java, so Java companies are constantly optimizing the implementation of String object in order to improve the performance of String object. Look at the following figure to understand the optimization process of String object.
1. In Java6 and earlier
String object is an object that encapsulates a char array. It mainly has four member variables: char array, offset, number of characters count, and hash value.
String object is to locate char[] array through offset and count two attributes, get string. This allows you to share array objects efficiently and quickly while saving memory space, but it is likely to cause memory leaks.
2. Starting with Java 7 to Java 8
Java has made some changes to the String class since Java 7. There are no longer two variables in the String class: offset and count. The benefit of this is that String objects consume slightly less memory, and String.substring methods no longer share char[], thus eliminating the memory leak problem that might result from using this method.
3. Starting with Java9
Change char[] array to byte[] array, why do you need to do this? We know that char is two bytes, and if it's a bit wasteful to store a byte of character, Java has changed it to a byte of string to save space. This avoids waste in storing one-byte characters.
Java9 maintains a new attribute coder, which is the identifier of the encoding format. When calculating the string length or calling the indexOf() function, you need to determine how to calculate the string length according to this field. By default, the coder attribute has two values: 0 for Latin-1 (single-byte encoding) and 1 for UTF-16 encoding. If String determines that the string contains only Latin-1, the coder attribute value is 0, otherwise it is 1.
String How objects are created
1. By means of string constants
String str= "pingtouge" form, when using this form to create a string, the JVM will first check whether the object exists in the string constant pool, if it exists, return the reference address of the object, if it does not exist, create the string object in the string constant pool and return the reference. The advantage of using this method is that it avoids creating strings with the same value repeatedly and saves memory.
String() constructor
String str = new String("pingtouge"), the process of creating string objects in this way is more complicated and divided into two stages. First, at compilation time, the string pingtouge will be added to the constant structure, and the string will be created in the constant pool when the class loads. Then, when new() is called, the JVM calls the String constructor, references the pingtouge string in the constant pool, creates a String object in heap memory, and returns the reference address in the heap.
Now that we know the two ways to create String objects, let's analyze the following code to deepen our understanding of these two ways. In the following code, is str equal to str1?
String str = "pingtouge"; String str1 = new String("pingtouge"); system.out.println(str==str1)
Let's analyze these lines of code one by one, starting with String str = "pingtouge", where a string object is created using string constants. When creating a pingtouge string object, the JVM will look for whether the string exists in the constant pool. The answer here is definitely no, so the 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 String str1 = new String("pingtouge"), where we create string objects using constructors. According to our understanding of creating string objects using constructors, 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 address reference of the pingtouge string in the heap, str must not equal str1.
String immutability of objects
From the moment we know about String objects, I think we all know that String objects are immutable. So how does it become immutable? What are the benefits of Java doing this? Let's discuss it briefly together. Let's first take a look at a piece of source code for String objects:
public final class String implements java.io.Serializable, Comparable, CharSequence { /** The value is used for character storage. */ private final char value[]; /** Cache the hash code for the string */ private int hash; // Default to 0 /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -6849794470754667710L; }
As can be seen from this source code, the String class uses the final modifier. We know that when a class is final, it indicates that the class cannot be inherited, so the String class cannot be inherited. This is the first point of String immutability.
Looking further down, the char value[] array used to store strings is decorated with private and final, and we know that for a variable of a primitive data type that is final, its value cannot be changed once initialized. This is the second point of String immutability.
Why Java companies set String to immutable is mainly considered from the following three aspects:
1. Ensure the security of String objects. Assuming the String object is mutable, the String object will likely be maliciously modified.
2. Ensure that hash attribute values do not change frequently, ensuring uniqueness, so that containers like HashMap can implement corresponding key-value caching functions.
3. String constant pool can be implemented
String object optimization
String is one of the Java types we commonly use, so the operation of string is also unavoidable, in the process of string operation, if used improperly, performance will be very different. So what do we need to pay attention to during string manipulation?
Elegant concatenation strings
String concatenation is one of the most frequently used string operations. Since we know that String objects are immutable, we use + as little as possible to concatenate strings or subconsciously think that string concatenation with + cannot be used, thinking that string concatenation with + will produce many useless objects. Is this really the case? Let's do an experiment. We use + to concatenate the following string.
String str8 = "ping" +"tou"+"ge";
How many objects does this code generate? If we analyze it as we understand it, we will first create the ping object, then create the pingtou object, and finally create the pingtouge object, which is a total of three objects. Is that true? In fact, this is not the case. Java company is afraid of our programmers 'mistakes, so it optimizes the compiler. The string concatenation above will be optimized by our compiler into a String str8 = "pingtouge"; object. In addition to optimizing constant string concatenation, the compiler also optimizes string concatenation dynamically using the + sign to improve String performance, such as the following code:
String str = "pingtouge";for(int i=0; i
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.