Detailed explanation of String, StringBuilder and StringBuffer

Detailed explanation of String, StringBuilder and StringBuffer


String class

Strings are widely used in Java programming. Strings are objects in Java. Java provides the String class to create and manipulate strings.

It should be noted that the value of String is immutable, which causes a new String object to be generated for each operation on String, which is not only inefficient, but also wastes a lot of limited memory space.

The String class is final, which means that the String class cannot be inherited, and its member methods are final by default. In Java, the final modified class is not allowed to be inherited, and the member methods in the class default to final methods. In the early version of the JVM implementation, the final modified method will be converted to inline calls to improve execution efficiency. Since Java SE5/6, this approach has been gradually abandoned. Therefore, in the current version of Java SE, there is no need to consider using final to improve method call efficiency. Only when you are sure that you do not want the method to be overwritten, set the method to final.

The String class actually saves strings through a char array.

Neither sub operation, concat nor replace operation is performed on the original string, but a new string object is regenerated. In other words, after performing these operations, the original string has not been changed.

Note: Always remember one thing here, "Any change to the String object will not affect the original object, and any related change operations will generate a new object."

Deep understanding of String

By analyzing the difference between String str="hello world" and String str=new String("hello world"), we can understand the String class in depth.

	public class Main {
    
    
	         
	    public static void main(String[] args) {
    
    
	        String str1 = "hello world";
	        String str2 = new String("hello world");
	        String str3 = "hello world";
	        String str4 = new String("hello world");
	         
	        System.out.println(str1==str2);
	        System.out.println(str1==str3);
	        System.out.println(str2==str4);
	    }
	}

The output result is:

false
true
false

What does this result represent?

There is a part of space in the class file to store the literal constants and symbol references generated during compilation. This part is called the class file constant pool, which corresponds to the runtime constant pool of the method area during runtime.

Therefore, in the above code, String str1 = "hello world"; and String str3 = "hello world"; both generate literal constants and symbol references during compilation, and during runtime the literal constant "hello world" is stored in the runtime constant pool (Of course only one copy is kept). In this way, if the String object is bound to a reference, the JVM execution engine will first look for the same literal constant in the runtime constant pool. If it exists, it will directly point the reference to the existing literal constant; otherwise, it will always be at runtime. The measurement pool opens up a space to store the literal constant and points the reference to the literal constant.
  In summary, the object generation by the new keyword is performed in the heap area, and the process of object generation in the heap area does not detect whether the object already exists. Therefore, when you create an object through new, you must create a different object, even if the content of the string is the same.


StringBuilder class and StringBuffer class

Unlike the String class, the objects of the StringBuffer and StringBuilder classes can be modified many times without generating new unused objects.

The StringBuilder class was proposed in Java 5. The biggest difference between it and StringBuffer is that the methods of StringBuilder are not thread-safe.

Since StringBuilder has a speed advantage compared to StringBuffer, it is recommended to use StringBuilder class in most cases. However, when the application requires thread safety, the StringBuffer class must be used.

If a StringBuffer object is used by multiple threads in the string buffer, many methods in StringBuffer can have the synchronized keyword, so the thread can be guaranteed to be safe, but the StringBuilder method does not have this keyword, so thread safety cannot be guaranteed , There may be some wrong operations. So if the operation to be performed is multi-threaded, then StringBuffer must be used, but in the case of single-threaded, it is recommended to use a faster StringBuilder.


to sum up

First of all, the running speed, or the execution speed, in this regard, the running speed is: StringBuilder> StringBuffer> String.
The reason for the slowest String is that String is a string constant, and both StringBuilder and StringBuffer are string variables, namely String Once the object is created, the object cannot be changed, and the objects of StringBuilder and StringBuffer are variables. To manipulate variables is to directly modify the object without creating and recycling operations, so the speed is much faster than String.
In terms of thread safety, StringBuilder is thread-unsafe, while StringBuffer is thread-safe.

String: Suitable for a small number of string operations.
StringBuilder: Suitable for a large number of operations in the character buffer under a single thread.
StringBuffer: Applicable to the situation where a large number of operations are performed on the character buffer under multithreading.


Guess you like

Origin blog.csdn.net/baidu_41847368/article/details/114699341