The difference between the String and StringBuffer classes in JAVA

  • 2020-05-26 09:15:43
  • OfStack

There are two types of string operations in Java: the String class and the StringBuffer class (the buffered string handling class).
So let's just say a little bit about the difference between 1 and 2.
The String class and the StringBuffer class both provide methods to implement string operations, but the two are slightly different.

(1) class String
Once this class 1 produces a string, its object is immutable. The content and length of the String class are fixed. If the program needs to get information about a string, it needs to call the various string manipulation methods provided by the system. Although strings can be manipulated through various system methods, this does not change the object instance itself, but instead generates a new instance. The system allocates memory for String class objects based on the actual number of characters the object contains.

(2) class StringBuffer
I looked up the word Buffer, which means buffered, and this class must have a buffered function. This class handles mutable strings. If you want to modify a string of class StringBuffer, you do not need to create a new string object, but directly manipulate the original string. The various string manipulation methods for this class are different from those provided by the String class. When the system allocates memory for the StringBuffer class, an additional 16-character-sized buffer is provided in addition to the space occupied by the current character. Each StringBuffer object has a fixed buffer capacity. When the string size does not exceed the capacity, no new capacity will be allocated. When the string size exceeds the capacity, the capacity will be automatically increased.

Here are some concrete examples

Concatenation of strings

The String class has two methods

Type 1 (" + ")


public class str{
  public static void main(String[] args){
      String str1=" Add special effects! ";
      String str2="Duang~~";
      System.out.println(str1+" "+str2);
    }
  }

Type 2 (" concat ")


public class str{
  public static void main(String[] args){
      String str1=" Add special effects! ";
      String str2="Duang~~";
      System.out.println(str1.concat(str2));
    }
  }

Method of class StringBuffer


public class str{
  public static void main(String[] args){
    // build 1 Object of a buffered string sb
    StringBuffer sb=new StringBuffer(" Add special effects! ");
    // through append Method, added after the object 1 Two new strings 
    sb.append(" Duang~~");
    System.out.println(sb);
  }
}

The final output is: plus effects! Duang ~ ~

From the above example, it is not difficult to see that when extending String class, it needs to instance two objects, each of which will occupy 1 fixed amount of memory, while StringBuffer class does not need to instantiate a new class, but only needs to call an extended method.

One more point is that the memory capacity of the StringBuffer class is expandable. Here's a concrete 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);         // Expansion of capacity 
      System.out.println(sb.capacity());   // Output string capacity capacity
    }
  }

The capacity() method represents the number of strings an object can hold in memory. If you want to expand the memory capacity, you can use the method ensureCapacity().


Related articles: