This article mainly introduces the differences between String, StringBuffer and StringBuilder in Java and related information on how to use it. The String class is often used during the development process. Friends who need it can refer to it
The difference between String, StringBuffer and StringBuilder in java and how to use it
1. String class
The value of String is immutable , which results in each operation on String generating a new String object, which is not only inefficient, but also wastes a lot of limited memory space.
String a = "a"; //Assume a points to address 0x0001
a = "b";//After reassignment, a points to address 0x0002, but 0x0001 The "a" saved in the address still exists, but it is no longer pointed to by a. A already points to another address.
Therefore, String operations are all about changing the assignment address rather than changing the value.
2. StringBuffer is a mutable class, and thread-safe string operation class. Any operation on the string it points to will not create a new object. Each StringBuffer object has a certain buffer capacity. When the string size does not exceed the capacity, new capacity will not be allocated. When the string size exceeds the capacity, the capacity will be automatically increased.
StringBuffer buf=new StringBuffer(); //分配长16字节的字符缓冲区 StringBuffer buf=new StringBuffer(512); //分配长512字节的字符缓冲区 StringBuffer buf=new StringBuffer("this is a test")//在缓冲区中存放了字符串,并在后面预留了16字节的空缓冲区。
3.StringBuffer
The functions of StringBuffer and StringBuilder classes are basically similar. The main difference is that the methods of StringBuffer class are Multi-threaded and safe, StringBuilder is not thread-safe. In comparison, the StringBuilder class will be slightly faster. For strings whose values frequently change, the StringBuffer and StringBuilder classes should be used.
1) First of all, String, StringBuffer, and StringBuilder are all defined as final classes in the JDK, which means that they cannot be inherited.
2) String is the most common. Compared with StringBuffer, String has poorer performance because a new String object will be regenerated when the String type is changed. This is obvious during string splicing operations. Therefore, strings whose content changes frequently should not use String. If multi-threading is not considered, StringBuilder should be used.
3) After StringBuffer generates an object, when performing string splicing operations, just call the append method. No new objects will be generated. Only the object itself will be operated, and the performance is higher than String. In addition, StringBuffer is thread-safe, so it is suitable for use in multi-threads. Because of this, the speed will be slower than StringBuilder.
4) The usage of StringBuilder is similar to StringBuffer, but it is not thread-safe, so it is generally used in single threads and is more efficient than StringBuffer.
In summary, which one to choose needs to be considered from many aspects such as memory performance, thread safety, execution efficiency, etc. The answer can be obtained from the above comparisons.
The above is the detailed content of Detailed explanation of the differences between String, StringBuffer and StringBuilder in Java. For more information, please follow other related articles on the PHP Chinese website!