The difference between String, StringBuffer and StringBuilder

The difference between String, StringBuffer and StringBuilder:

Article directory

String

[The String class] is an immutable class, that is, once a String object is created, the sequence of characters contained in the object is immutable until the object is destroyed.

This is the explanation of the String class. Xiao Xianer saw this situation before and could not understand the above explanation, as follows

String a = "123";
a = "456" ;
 // The printed a is 456 System.out 
.println (a)

Seeing this, Xiao Xian’er doesn’t understand, isn’t it obvious that he has been modified? Why do you say he is an immutable class?

After the study of Xiao Xianer and his friends, I understand where the immutability of the String class is reflected. Next, let’s look at a memory storage space map of the above a object.

It can be seen that when assigning a value to a again, instead of reassigning the instance object in the original heap, a new instance object is generated and points to the string “456”, and a points to the newly generated instance object. The instance object still exists and will be garbage collected if not referenced again.

[StringBuffer]

The StringBuffer object represents a string with a variable character sequence. When a StringBuffer is created, the string can be changed through the append(), insert(), reverse(), setCharAt(), setLength() methods provided by StringBuffer. The character sequence of the object. Once the final desired string has been generated from the StringBuffer, its toString() method can be called to convert it to a String object.

StringBuffer b = new StringBuffer("123");
b.append( "456" );
 // b print result is: 123456 System.out 
.println (b);

Take a look at the memory space diagram of the b object:

So the StringBuffer object is a string with a variable character sequence, it does not regenerate an object, and a new string can be concatenated in the original object.

StringBuilder

The StringBuilder class also represents mutable string objects. In fact, StringBuilder and StringBuffer are basically similar, and the constructors and methods of the two classes are basically the same. The difference is: StringBuffer is thread-safe, while StringBuilder does not implement thread-safety, so the performance is slightly higher.

How does StringBuffer achieve thread safety?

Methods implemented in the StringBuffer class:

Methods implemented in the StringBuilder class:

It can be seen that the synchronized keyword   is added to the methods in the StringBuffer class, that is, a lock is added to this method to ensure thread safety.

Improvements in Java9

Java9 has improved the implementation of strings (including String, StringBuffer, StringBuilder). Before Java9, strings used char[] arrays to store characters, so each character of the string accounted for 2 bytes; while Java9 strings used byte[] arrays and an encoding-flag field to store characters, so strings Each character occupies only 1 byte. So Java9’s strings are more space-saving, and the functional methods of strings are not affected.

Leave a Comment

Your email address will not be published. Required fields are marked *