One, immutable string type
String is the most common behavior in computer programming, the main class of Java string manipulation is string, and the string object is immutable (immutable), that is, once the object is created in memory, its contents are no longer changed. Although there are many methods available in the string class that look like you can modify a string object, such as trim (), subString (), and so on, they do not actually change the original string object, which passes only one copy of the reference. Therefore, a string object was recreated and a new reference was made.
For example, the following code illustrates the immutable nature of a string:
Package Date0804.demo2; public class Immutablestring {public static void Main (string[] args) { string str=new string ("xyz"); Change (str); System.out.println (str); } public static void Change (String s) { s= "xml"; }}
It outputs the result of
Str=xyz
Therefore, I can see that whenever a string object is used as a parameter to a method, a reference is copied, and the object referred to by that reference is always in a single physical location and has never been changed.
In addition, the string object supports the + operator, and the + operator can combine strings, but according to a related study, using the + operator to concatenate strings can degrade efficiency, so when it comes to string changes, the following StringBuilder or StringBuffer are preferred , both provide rich string processing commands.
Ii. Types of StringBuilder
The StringBuilder type is a variable string type, and the StringBuilder type API is basically consistent with the StringBuffer type of API, the only difference being that the StringBuilder usage is assumed to be in a single thread, In other words, StringBuilder is not thread-safe. StringBuilder usually sets a capacity by default when instantiating, typically the length of the string parameter is +16. StringBuilder is an abstract class that inherits Abstractstringbuilder, and this abstract class is implemented internally using a character array that can be dynamically extended. Common methods provided by the StringBuilder class are append (), insert (), replace (), Deletecharat (), indexOf (), reverse (), toString (), and so on. It can realize the basic functions of adding and deleting strings.
Package Date0812.demo1;public class Test3 {public static void main (string[] args) {StringBuilder StringBuilder = new Strin Gbuilder ("Eclipse");//Add StringBuilder = Stringbuilder.append ("Software");//capacity int C = stringbuilder.capacity ();// Flip stringbuilder= stringbuilder.reverse (); System.out.println (C); System.out.println (StringBuilder);}} Running Result: 23erawtfos Espilce
Iii. Types of StringBuffer
StringBuffer is a variable, thread-safe type that handles strings. Similarly, StringBuffer also inherits Abstractstringbuilder abstract classes, so the interior also uses character arrays to store strings. The difference with StringBuilder is that the modifier for most methods of the StringBuffer type is prefixed with the keyword synchronized, including length (), Capacity (), TrimToSize (), append (), Delete (), insert (), and so on, which indicates that StringBuffer is thread-safe and can be used for multi-threaded concurrency, all concurrent operations on StringBuffer are executed in a certain order, ensuring that they are executed correctly, on the other hand, It also shows that the cost of StringBuffer is a little bigger than StringBuilder. StringBuffer is developed earlier than StringBuilder, and in a single thread, you can use StringBuilder to increase the speed of the operation and use the StringBuffer type if you encounter multiple threads that require synchronization. The other, in the API call aspect, basically identical.
String, StringBuilder, StringBuffer features of Java strings