This article brings you relevant knowledge about java, which mainly introduces related issues about the string class, including the constant pool of strings, the immutability of strings, etc. Let’s take a look at the content below. I hope it will be helpful to everyone.
Recommended study: "java Video Tutorial"
First of all, let’s take a look at the source code of the String class in the JDK. It implements many interfaces. You can see that the String class is modified by final. This means that the String class cannot be inherited and there are no subclasses of String. In this way, everyone who uses JDK uses the same String class. If String is allowed to be inherited, everyone can extend String. Everyone uses different versions of String, and two different people use the same String class. Methods show different results, which makes it impossible to develop the code
While inheritance and method overriding bring flexibility, they also bring about many problems with inconsistent behavior of subclasses
Method 1: Direct assignment (commonly used)
String str = " hello word "
Method 2: Pass Constructor method generates objects
String str1 = new String(" hello word ");
Method 3: Generate objects through character array
char[] data = new char[]{'a', 'b' ,'c'};
Method 4: Convert to string through String's static method valueOf (any data type) = > (commonly used)
String str2 = String.valueOf(10 );
Literal value: The value written directly is called literal value
10 – > int literal value
10.1 --> double literal
true --> boolean literal
" abc " – > String literal
The literal of a string is actually a string object
String str = “hello word”;
String str2 = str;
At this time, this is both a string literal and a string object. In order to facilitate understanding, draw a picture. This is to facilitate our understanding. For the time being, we think it is stored on the heap. In fact,
is stored in the method area. If we let str2 = "Hello" at this time, it will not affect the output of str because it is enclosed by " ". Hello is also a string object, which means that a new space is opened on the heap at this time, and str2 saves the address space of the new object at this time, which has no effect on str
When comparing whether all reference data types are equal, use the equals method to compare. The common classes in JDK have overridden the equals method, and you can use it directly
Reference data types What is compared using == is the address
The picture below shows two references pointing to the same address space, which is related to the constant pool of strings
The picture below produces two objects and two addresses Space, using == returns false
The comparison of equals is a case-sensitive comparison
The equalsIgnoreCase method is a case-insensitive comparison
When used When the direct assignment method generates a string object, the JVM will maintain a string constant pool. If the object does not exist in the heap, a string object will be generated and added to the string constant pool; when the direct assignment method continues to be used When generating a string object, the JVM finds that the content pointed to by the reference already exists in the constant pool. At this time, it no longer creates a new string object, but directly reuses the existing object. This is why the three in the figure above The reference points to the same block address
When the object is generated for the first time, there is nothing in the constant pool, so a string object is generated and stored in the constant pool. When the second When the object is generated three times, the JVM finds that the same content already exists in the constant pool, so it no longer generates a new object and directly points to the same address space as str1
The program is executed from right to left. At this time, the right side of the first line of code is a string constant, which is also a string object, so first open up a space in the constant pool, and then create a new one The string object is stored, and the program is executed to the left. When the new keyword is encountered, a new object is created and stored in the heap. Then str1 points to the object in the heap. When pointing to the second and third lines of code, the constant pool is found. The object already exists in the object and will not be created again. When the new keyword is encountered, a new object will be created. The memory diagram is as follows:
String class provides The intern method, which is a local method:
Calling the intern method will save the object pointed to by the current string reference to the string constant pool. There are two situations:
1. If If the object already exists in the current constant pool, no new object will be generated and the String object in the constant pool will be returned.
2. If the object does not exist in the current constant pool, the object will be put into the pool and returned to the pool. the following address.
1. Take a look at the output of the following lines of code
Because the intern method has a return value, str1 only calls the intern method at this time and does not receive the return value. So str1 still points to the object in the heap, str2 points to the object in the constant pool, so false is returned;
As long as the return value of calling the intern method is received, true will be returned;
At this point, the object pointed to by str1 is manually added to the pool. The object already exists in the pool. Directly let str1 point to the object.
2. Take a look at the following lines of code Output
When manually entering the pool, there is nothing in the pool, so it is moved directly into the constant pool
Note: The so-called string immutability means that the content of the string is immutable, and References that are not strings cannot be changed
The immutable here refers to "hello", "world", "helloworld", "!!!", and the spliced "helloworld!!!" "These already created string objects cannot modify their contents once declared, but the references can be changed. One will point to hello, another will point to helloworld, and another will point to hello world! ! ! , this is all possible
A string is just a character array -> char[], the string is actually stored in the character array. Why can't the content of the string be changed? Let's take a look at the source code of the string and find out.
We can see that the character array inside String is encapsulated. This character array cannot be accessed from outside the String class, let alone changing the content of the string
String str = " hello ";
1. Destroy the encapsulation of the value array through reflection at runtime
2. Use StringBuilder or StringBuffer instead Class - - is no longer a type
a.StringBuilder: thread-safe, strong performance
b.StringBuffer: thread-safe, poor performance
In addition, the usage of the two classes is exactly the same
If you need to splice strings frequently, use the append method of the StringBuilder class. Only one object is generated here, which will become hello for a while and hello world for a while.
The StringBuilder class and String are two independent classes. The StringBuilder class was created to solve the problem of string splicing.
Mutual conversion between the StringBuilder class and the String class :
1.StringBuilder becomes String class and calls toString method
2.String class is transformed into StringBuilder class, use StringBuilder's constructor or append method
Other commonly used methods:
a. String reversal operation, reverse() provided by sb;
b. Delete the specified range of data, delete (int start, int end); delete all content starting from start to end, closed on the left and open on the right
c. Insert operation, insert (int start, various data types): Start inserting from the start index position. The starting index of the insertion is start
Recommended learning: "java video tutorial"
The above is the detailed content of Completely master Java's String class. For more information, please follow other related articles on the PHP Chinese website!