Differences and usage between string and StringBuffer in Java and StringBuilder

Differences and usage between string and StringBuffer in Java and StringBuilder

1. String class

The value of string is immutable, which leads to the generation of new string objects every time you operate on string, which is not only inefficient, but also a large waste of limited memory space.

String a = "a"; // Suppose a points to address 0x0001

a = "b";// After re assignment, a points to the address 0x0002, but the "a" saved in the address 0x0001 still exists, but it is no longer pointed to by a, and a has pointed to other addresses.

Therefore, the operation of string is to change the assignment address rather than the value.

2. StringBuffer is a variable class and thread safe string operation class. Any operation on the string it points to will not produce a new object. Each StringBuffer object has a certain buffer capacity. When the string size does not exceed the capacity, no new capacity will be allocated. When the string size exceeds the capacity, the capacity will be automatically increased.

3.StringBuffer

The functions of StringBuffer and StringBuilder classes are basically similar. The main difference is that the methods of StringBuffer class are multithreaded and safe, while StringBuilder is not thread safe. In comparison, StringBuilder class will be slightly faster. For strings that often change values, you should use StringBuffer and StringBuilder classes.

1) First, string, StringBuffer and StringBuilder are defined as final classes in JDK, which means that they cannot be inherited.

2) String is the most common. Compared with StringBuffer, the performance of string is poor, because a new string object will be regenerated when changing the string type, which is obvious in string splicing operation. Therefore, strings with frequently changed contents should not be used. If multithreading is not considered, StringBuilder should be used.

3) After an object is generated by StringBuffer, the append method can be called during string splicing. No new object will be generated. Only the object itself will be operated. The performance is higher than that of string. In addition, StringBuffer is thread safe, so it is suitable for multithreading. Because of this, the speed is slower than StringBuilder.

4) The use method of StringBuilder is similar to that of StringBuffer, but it is non thread safe. Therefore, it is generally used for single thread, and its efficiency is higher than that of StringBuffer.

To sum up, which one to choose needs to be considered from memory performance, thread safety, execution efficiency and other aspects. The answer can be obtained from the above comparisons.

The above is the difference between Java string and StringBuffer and StringBuilder. If you have any questions, please leave a message or go to the community of this site for communication and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>