String, StringBuffer and StringBuilder in Java (Summary)

The String class is a class commonly used to operate on strings in java, and all string literals in java programs can be implemented as instances of this class.
In addition, there are two classes, StringBuffer and StringBuilder, which can operate on strings. The three classes mentioned above will be summarized below.

String

Understanding String

String is different from the eight basic data types stipulated in Java. It belongs to the reference type other than these. When it is created, it must first allocate a piece of memory for its reference (handle) on the stack, and the specific information of its reference is stored. On the heap memory, then the reference on the stack points to the address of the object in the heap. *Reference type: It is the data type represented by the actual value reference of the type (the function can be simply compared to the pointer of C language, but a small memory space is opened on the stack to save an address, but there is no direct access in the
pointer concept of memory)

Sting str = "hello word";

insert image description here

how to create a string

Several commonly used construction methods:

// 方式一
String str = "Hello wold";
// 方式二
String str2 = new String("Hello wold");

Partial interception of the String constructor provided in the official document
insert image description here

Comparison of Sting strings

Because it is a reference type, when two variables created by this type are compared using ==, it is different from the basic data type, and the comparison is whether the two references point to the same object, and you want to compare the objects it represents When the values ​​are equal, you need to use the equals method under the String class

String str1 = new String("hello word")String str2 = new String("hello word");
System.out.println(str1 == str2);//false
System.out.println(str1.equals(str2));//true

insert image description here

Understanding of character constant pool

When designing the String class in Java, a design pattern- flyweight pattern is used (that is, to provide a solution to the reuse of objects, and to use sharing technology to reuse the same or similar objects).
In the JVM virtual machine, the bottom layer maintains a The area is used to store strings, that is, the string constant pool
. If the method of direct assignment is used to create a String string, the instantiated object will be automatically saved in the string constant pool and
created by the same method next time. The object that uses this value in the string constant pool will directly refer to the object, and then create it anyway
insert image description here

ps: If you use the constructor to create a string (new String("abc")), two memory spaces will be opened up on the heap and will not be automatically saved in the string constant pool

The difference between the two instantiated objects of the String class:

Create by direct assignment (String str = "abc"): only open up a memory space, and the string object will be automatically saved in the string constant pool for the next creation using the constructor to create (String str = new String("
abc ”)): Two memory spaces will be opened up. The new object is in the common area of ​​the heap. The created string is in the constant pool and will not be automatically saved in the string constant pool. If you enter the pool, you need to call String’s intern( )method

Why is the String string immutable?

1. It is convenient to realize the string constant pool. If it is immutable, there is no need to consider the problem of deep and shallow copying.
2. Because the object is immutable, there is no need to consider thread safety issues
. 3. Immutable is more convenient to cache hashCode, which can be efficiently saved in HashMap as a key.

Commonly used APIs of the String class
insert image description here

StringBuffer与StringBuilder

Since the string declared by String itself is immutable, if you want to change its content, you can only change its reference point, so for the convenience of String string modification, Java provides two classes StringBuffer and StringBuilder to operate

Most of the functions of StringBuffer and StringBuilder are the same, and some common APIs are introduced below

Constructor:
insert image description here
Modifier:
insert image description here
String to StringBuffer/StringBuilder needs to use the construction methods of StringBuffer and StringBuilder
StringBuffer and StringBuilder to String directly use the toString() method

The difference between String, StringBuffer and StringBuilder

  1. The content of String cannot be modified, but the content of StringBuffer and StringBuilder can be modified
  2. StringBuffer implements thread safety with synchronization lock; StringBuilder does not implement thread safety
  3. The toString() method of StringBuffer adds a buffer, and StringBuilder directly copies a character array to build a string
  4. Since StringBuilder does not implement thread safety, its methods do not add synchronization locks, making the execution efficiency of StringBuilder much higher than that of StringBuffer

The above is a summary of the knowledge points of String. With the deepening of follow-up study, the content will be supplemented and modified synchronously. It will be a great honor to help all bloggers. Please correct me

Guess you like

Origin blog.csdn.net/m0_46233999/article/details/109623723