Beginner's knowledge of JAVA (2) ---- difference between the String class and the StringBuffer class, javastringbuffer
There are two types of String operations in Java: String and StringBuffer (buffer String processing ).
The following is a brief description of the differences between the two.
Both the String class and the StringBuffer class provide corresponding methods for String operations, but the two are slightly different.
(1) String class
Once the class generates a string, its object is not variable. The content and length of the String class are fixed. If the program needs to obtain string information, it needs to call various string operation methods provided by the system. Although operations can be performed on strings through various system methods, this does not change the object instance itself, but instead generates a new instance. The system allocates memory for String objects based on the actual number of characters contained in the objects.
(2) StringBuffer class
I checked the word Buffer, which indicates buffering. This class must have the Buffer function. This class processes variable strings. To modify a StringBuffer string, you do not need to create a new String object, but directly operate on 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 character, it also provides a buffer of another 16 characters. Each StringBuffer object has a certain buffer capacity. When the string size does not exceed the capacity, no new capacity is allocated. When the string size exceeds the capacity, the capacity is automatically increased.
The following are some examples.
String connection
There are two methods for the String class
First ("+ ")
Public class str {public static void main (String [] args) {String str1 = "add special effect! "; String str2 =" Duang ~~ "; System. out. println (str1 +" "+ str2 );}}
Second ("concat ")
Public class str {public static void main (String [] args) {String str1 = "add special effect! "; String str2 =" Duang ~~ "; System. out. println (str1.concat (str2 ));}}
StringBuffer Class Method
Public class str {public static void main (String [] args) {// construct a buffer String object sb StringBuffer sb = new StringBuffer ("with special effects! "); // Add a new string sb. append (" Duang ~~ "); System. out. println (sb );}}
The final output result is: add special effects! Duang ~~
The preceding example shows that the String class requires two instances for expansion. Each object occupies a certain amount of memory, and the StringBuffer class does not need to instantiate a new class, you only need to call an extension method.
Another point is that the memory capacity of the StringBuffer class is scalable. For example:
Public class str {public static void main (String [] args) {// declare the String object sb StringBuffer sb = new StringBuffer (40); System. out. println (sb. capacity (); // capacity of the output string capacity sb. ensureCapacity (100); // expand the capacity System. out. println (sb. capacity (); // capacity of the output string capacity }}
The capacity () method represents the number of strings that a String object can contain in the memory. To expand the memory capacity, use ensureCapacity ().