Java essays String, StringBuffer and StringBuilder classes

The difference between String, StringBuffer and StringBuilder

Two differences in the creation of String objects

Summary: The instance of the String type is a fixed-length (once immutable) string. If a String string variable is directly assigned, it is regarded as a string constant variable stored in the "permanent generation", and multiple strings have the same constant Variables share a piece of memory. When a string variable calls new String("") to create a variable, because new is called, the variable is stored in the "new generation" and occupies a piece of memory alone. After jdk1.8, the permanent generation is called Metasspace

` String text1 = “1234567”;
String text2 = “1234567”;
String text3 = new String(“1234567”);
String text4 = new String(“1234567”);

    System.out.println(text1 == text2);
    System.out.println(text1 == text3);
    System.out.println(text1 == text4);
    System.out.println(text3 == text4);`

If "1234567" is not assigned to a variable, it is an anonymous object, that is, an object of the String class can directly call the String method. Because "1234567" is an anonymous string constant object address assigned to text1 and text2 (instances of the String class store strings are actually stored through the char[] array, and are stored in the string constant pool in theoretical memory The method area is actually in the permanent generation heap), so it is obvious that text1 and text2 are string constant variables and they share a piece of memory, that is, text1 == text2. But String text3 = new String("1234567");
String text4 = new String("1234567");
Obviously, this 1234567 string is stored in the newly generated memory by new, and the value of text3 is the newly generated memory address, text4 The same is true, and text3 and text4 are two pieces of memory. So it is not difficult to foresee text3 != text4. The
Insert picture description here
running result is shown in the figure

The difference between the two ways of concatenating multiple strings of String

The first is the simplest and most convenient + sign, and the second is to call append(String str) method

`String text5 = "Noon on the day of hoeing the grass";
String text6 = "Sweat dripping under the soil";
String text7 = "Moonlight in
front of the bed"; text5 = text5+text6+text7;
System.out.println(text5);

    StringBuilder sb = new StringBuilder();
    sb.append("锄禾日当午");
    sb.append("汗滴禾下土");
    sb.append("床前明月光");
    String text8 = sb.toString();
    System.out.println(text8);`

The first method: text5+text6 will generate a new string and then have an address in memory, (text5+text6)+text7 will generate a new string with an address in memory, and the total new string address is assigned to After text5, the string pointed to by the original text5 becomes garbage, and the generated string (text5+text6) also becomes garbage. Because the string stored in the string variable declared by the String type is a constant in the string constant pool, The garbage collector GC stored in the permanent generation will not collect it in time. It can be seen that when using the + sign to splice multiple strings, a lot of new strings will become garbage and occupy memory space. Try your best when there are many string data to be spliced. Avoid using + to concatenate strings!

The second method: There are append methods in the StringBuilder and StringBuffer classes, but the String class does not. The append method returns StringBuilder or StringBuffe objects. Stringbuffer is actually a dynamic string array and append() is to add to the dynamic string array. String text1+text2 is to concatenate two strings and place them in a new memory address. Then StringBuffer and StringBuilder are dynamic arrays. After splicing, the memory address is still the first address of the dynamic array, so there is no memory garbage occupied. Since append returns an object, Xu Yao calls the toString method to turn it into a string and print it out.

As for the methods of StringBuffer and StringBuilder are basically the same, the two operations are the same when spliced ​​together, and the main differences between the three are:
StringBuilder multi-threaded simultaneous operation is not safe; (read and write errors and deadlocks may occur) Situation)
The realization of StringBuffer multi-thread safety;
String is single-threaded and cannot be operated by multiple threads at the same time.

It is recommended to read why strings in Java are defined as immutable to enhance understanding

Guess you like

Origin blog.csdn.net/qq_40694640/article/details/112598478