Home > Java > javaTutorial > body text

The difference between String class and StringBuffer class in JAVA

高洛峰
Release: 2017-01-21 16:31:32
Original
1377 people have browsed it

There are two types of string operations in Java: String class and StringBuffer class (buffered 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 they are slightly different.

(1) String class
Once this class generates 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 needs 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 based on the actual number of characters contained in the object.

(2) StringBuffer class
Looked up the word Buffer, it means buffering, this class must have buffering function. This class handles mutable strings. If you want to modify a string of the StringBuffer class, you do not need to create a new string object, but directly operate on the original string. The various string manipulation methods of this class are not the same as 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 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.

The following are some specific examples

Concatenation of strings

The String class has two methods

The first one ("+")

public class str{
  public static void main(String[] args){
      String str1="加特效!";
      String str2="Duang~~";
      System.out.println(str1+" "+str2);
    }
  }
Copy after login

Second type ("concat")

public class str{
  public static void main(String[] args){
      String str1="加特效!";
      String str2="Duang~~";
      System.out.println(str1.concat(str2));
    }
  }
Copy after login

Methods of StringBuffer class

public class str{
  public static void main(String[] args){
    //构建一个缓冲字符串的对象sb
    StringBuffer sb=new StringBuffer("加特效!");
    //通过append方法,在这个对象后面添加一个新字符串
    sb.append(" Duang~~");
    System.out.println(sb);
  }
}
Copy after login

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 instantiate two objects. Each object will occupy a certain amount of memory, and the StringBuffer class does not need to instantiate another one. For a new class, you only need to call an extended method.

Another point is that the memory capacity of the StringBuffer class is scalable. To give a specific example:

public class str{
  public static void main(String[] args){
     //声明字符串对象sb
      StringBuffer sb=new StringBuffer(40);
      System.out.println(sb.capacity());   //输出字符串的容量capacity
      sb.ensureCapacity(100);         //扩充容量
      System.out.println(sb.capacity());   //输出字符串的容量capacity
    }
  }
Copy after login

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

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.

For more articles related to the difference between the String class and the StringBuffer class in JAVA, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template