Home > Java > javaTutorial > body text

Completely master Java's String class

WBOY
Release: 2022-05-25 13:41:33
forward
1661 people have browsed it

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.

Completely master Java's String class

Recommended study: "java Video Tutorial"

1. Understanding String

1.JDK String

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
Completely master Javas String class

2. Four ways to create a string

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 );
Completely master Javas String class

3. String literal value

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,
Completely master Javas String class
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
Completely master Javas String class

4 .String comparison is equal

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
Completely master Javas String class
The picture below produces two objects and two addresses Space, using == returns false
Completely master Javas String class
The comparison of equals is a case-sensitive comparison
Completely master Javas String class
The equalsIgnoreCase method is a case-insensitive comparison
Completely master Javas String class

2. String constant pool

1. What is the string constant pool

Completely master Javas String class
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
Completely master Javas String class
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

Completely master Javas String class
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:
Completely master Javas String class

2. Manual pooling method

String class provides The intern method, which is a local method:
Completely master Javas String class
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
Completely master Javas String class
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;
Completely master Javas String class
As long as the return value of calling the intern method is received, true will be returned;
Completely master Javas String class
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.
Completely master Javas String class
2. Take a look at the following lines of code Output
Completely master Javas String class
When manually entering the pool, there is nothing in the pool, so it is moved directly into the constant pool
Completely master Javas String class


3. The immutability of strings

1. Why is it immutable

Note: The so-called string immutability means that the content of the string is immutable, and References that are not strings cannot be changed
Completely master Javas String class
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
Completely master Javas String class
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.
Completely master Javas String class
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 ";
Completely master Javas String class

2. How to modify the string content

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.
Completely master Javas String class

3. Specific use of the StringBuilder class

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
Completely master Javas String class

2.String class is transformed into StringBuilder class, use StringBuilder's constructor or append method
Completely master Javas String class
Completely master Javas String class
Other commonly used methods:
a. String reversal operation, reverse() provided by sb;
Completely master Javas String class

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
Completely master Javas String class

c. Insert operation, insert (int start, various data types): Start inserting from the start index position. The starting index of the insertion is start
Completely master Javas String class

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template