Introduction to String Constant Pool, StringBuilder and StringBuffer in Java (String Constant Pool Interview Questions)


foreword

The text mainly introduces the relevant knowledge of the string constant pool in Java, and the difference between StringBuilder and StringBuffer.


1. String constant pool

1. What is a string constant pool?

Strings can be created directly by initialization.
like:

public static void main(String[] args) {
    
    
 String s1 = "hello";  //直接创建
 String s2 = "hello";
 System.out.println(s1 == s2); // true
}

Result: S1==S2, that is, the two strings point to the same object.
For reference data types, == compares addresses.
Question: Why are the addresses of S1 and S2 equal?

(1) String constant pool : In order to avoid generating a large number of string objects, make the program code run faster and save memory. Java has designed a string pool (String Pool), which stores frequently used literal constants such as 1, 2, "hello", etc. in it. The string constant pool is the StringTable class
in the JVM , which is actually a fixed-size HashTable. The location and default size of the string constant pool under different JDK versions. (2) Note:
insert image description here

insert image description here

2. The difference between the two object instantiations in the String class

String s1 = "hello";//第一种
String s2 = new String("hello");//第二种

The first one: only a piece of heap memory space will be opened up and stored in the string constant pool, and then s1 will share the String objects in the constant pool.
The second type: two heap memory spaces will be opened up, the string "hello" will be stored in the string constant pool, and then the newly opened String object will be assigned a value with the String object in the constant pool.

3. Other constant pools

Class file constant pool: After each Java source file is compiled, the .Class file will save the literal constants and symbol information in the current class.
Runtime constant pool: When the .Class file is loaded, the constant pool in the .Class file is loaded into memory called the runtime constant pool, and each class of the runtime constant pool has a copy.

4. String constant pool interview questions

(1) How many objects does String s = new String("hello") create?

① 1 string constant pool: "hello"
② 1 object created on the heap: new String("hello")
③ 1 s reference (assign the value of the object to s)
to create a total of: 3 objects

(2) String s = new String("A"+"B") ; How many objects are created?

① 3 string constant pools: "A", "B", "AB"
② 1 object created on the heap: new String("AB")
③ 1 s reference (assign the value of the object to s)
in total Created: 5 objects

(3) String s = new String("ABC") + "ABC" ; How many objects are created?

① 1 string constant pool: "ABC"
② 1 object created on the heap: new String("ABC")
③ 1 s reference (assign the value of the object to s)
to create a total of: 3 objects


二、StringBuilder、StringBuffer

(1) Since String is an immutable object, the content in the string cannot be changed.
Note: All operations involving the String class that may modify the content of the string are to create a new object, and the new object is changed.
(2) In order to facilitate the modification of strings, Java also provides StringBuilder and StringBuffer classes.
Unlike the String class, objects of the StringBuffer and StringBuilder classes can be modified multiple times without generating new unused objects .

1. StringBuilder

Common methods of StringBuilder are introduced below:
insert image description here
Note:
① String and StringBuilder classes cannot be converted directly. If you want to convert each other, you can use the following principles:
② String becomes StringBuilder: use the StringBuilder construction method or append() method
③ StringBuilder becomes String: call the toString() method.

2. The difference between String, StringBuffer and StringBuilder

① The content of String cannot be modified, but the content of StringBuffer and StringBuilder can be modified.
② Most functions of StringBuffer and StringBuilder are similar.
③ StringBuffer uses synchronous processing, which is a thread-safe operation; while StringBuilder does not use synchronous processing, which is a thread-unsafe operation
.


Summarize

That's all for this article.

Guess you like

Origin blog.csdn.net/m0_53689542/article/details/124561774