When you modify a string, you need to use the StringBuffer and StringBuilder classes.
Unlike the String class, objects of the StringBuffer and StringBuilder classes can be modified more than once, and no new unused objects are produced.
The StringBuilder class is presented in Java 5, and the biggest difference between it and StringBuffer is that the StringBuilder method is not thread-safe (cannot be accessed synchronously).
Since StringBuilder has a speed advantage over StringBuffer, it is recommended to use the StringBuilder class in most cases. However, in cases where the application requires thread safety, the StringBuffer class must be used.
StringBuilder |
Thread not secure |
Fast speed |
StringBuffer |
Thread Safety |
Slow speed |
Public class test{ publicstaticvoid main (String args[]) { new StringBuffer ("Novice Tutorial official Website:"); Sbuffer.append ("www"); Sbuffer.append (". Runoob"); Sbuffer.append (". com"); System.out.println (Sbuffer); }}
The results of the above example compilation run as follows:
Novice Tutorial Official website: www.runoob.com
StringBuffer Method
The following are the main methods supported by the StringBuffer class:
The methods in the following list are similar to the methods of the String class:
JAVA StringBuffer and StringBuilder classes