There are two kinds of string operations in Java: String class and StringBuffer class (buffer string processing class).
Let’s briefly talk about the difference between the two.
Both the String class and the StringBuffer class provide corresponding methods to implement string operations, but the two are slightly different.
(1) String class
Once this class produces a string, its object is immutable. The content and length of the String class are fixed. If the program needs to obtain string information, it is necessary to call various string operation methods provided by the system. Although operations can be applied to strings through various system methods, this does not change the object instance itself, but generates a new instance. The system allocates memory for String class objects, which are allocated according to the actual number of characters contained in the object.
(2) StringBuffer class
I checked the word Buffer, which means buffering, and this class must have a buffering function. This class handles variable strings. If you want to modify a StringBuffer class string, you don’t need to create a new string object, but directly operate the original string. The various string operation methods of this class are different from those provided by the String class. When the system allocates memory for the StringBuffer class, in addition to the space occupied by the current characters, it also provides an additional 16 character buffer. 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.
Here are some specific examples
String connection
There are two methods for the String class
The first type ("+")
public class str{ public static void main(String[] args){ String str1="Add special effects!"; String str2="Duang~~"; System.out.println(str1+" "+str2); } }The second type ("concat")
public class str{ public static void main(String[] args){ String str1="Add special effects!"; String str2="Duang~~"; System.out.println(str1.concat(str2)); } }Methods of StringBuffer class
public class str{ public static void main(String[] args){ //Construct an object that buffers strings sb StringBuffer sb=new StringBuffer("Add special effects!"); //Use the append method to add a new string sb.append("Duang~~"); System.out.println(sb); }}The final output results are: add special effects! Duang~~
It is not difficult to see from the above example that when the String class is extended, it needs to instance two objects, each object will occupy a certain amount of memory. The StringBuffer class does not need to instantiate a new class, it only needs to call an extended method.
There is also a point where the memory capacity of the StringBuffer class is scalable. Let's give a specific example:
public class str{ public static void main(String[] args){ //Declare string object sb StringBuffer sb=new StringBuffer(40); System.out.println(sb.capacity()); //Output string capacity capacity sb.ensureCapacity(100); //Expand capacity System.out.println(sb.capacity()); //Output string capacity capacity } }The capacity() method represents the number of string objects that can accommodate strings in memory. If you want to expand the memory capacity, you can use the method ensureCapacity().
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.