Beginner Java (ii)----The difference between a string class and a StringBuffer class

Source: Internet
Author: User

There are two kinds of string operations in Java: The String class and the StringBuffer class (the buffered string processing Class).
Let's talk about the difference between the two briefly.
Both the string class and the StringBuffer class provide methods for implementing string operations, but they are slightly different.

(1) String class
Once the class produces a string, its object is immutable. The contents and length of the string class are fixed. If the program needs to get information about the string, it needs to invoke the various string manipulation methods provided by the system implementation. Although it is possible to manipulate strings by various system methods, this does not change the object instance itself, but rather creates a new instance. The system allocates memory for the string class object, which is allocated according to the actual number of characters that the object contains.

(2) StringBuffer class
Check the word buffer, there is buffering meaning, this class must have buffering function. This class handles variable strings. If you want to modify a string of a StringBuffer class, you do not need to create a new string object, but instead directly manipulate the original strings. The various string manipulation methods of this class are different from the methods provided by the string class. When the system allocates memory for the StringBuffer class, it also provides an additional 16-character-size buffer, except for the space occupied by the current character. Each StringBuffer object has a certain buffer capacity, and when the string size does not exceed the capacity, no new capacity is allocated and the capacity is automatically increased when the string size exceeds the capacity.

Here are some concrete examples

Connection of strings

There are two methods of the String class

The first type ("+")

publicclass str{    publicstaticvoidmain(String[] args){            String str1="加特效!";            String str2="Duang~~";            System.out.println(str1+" "+str2);        }    }

The second type ("Concat")

publicclass str{    publicstaticvoidmain(String[] args){            String str1="加特效!";            String str2="Duang~~";            System.out.println(str1.concat(str2));        }    }

Methods of the StringBuffer class

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

The result of the final output is: Add effects! duang~~

The above example is not difficult to see, the string class in the expansion of the time, the need to instance two objects, each object will occupy a certain amount of memory, and the StringBuffer class does not need to instantiate a new class, only need to invoke an extension of the method can be.

And a little bit of stringbuffer. The memory capacity of the class is extensible. To give a specific example:

publicclass str{    publicstaticvoidmain(String[] args){          //声明字符串对象sb            StringBuffer sb=new StringBuffer(40);            System.out.println(sb.capacity());      //输出字符串的容量capacity            sb.ensureCapacity(100);                 //扩充容量            System.out.println(sb.capacity());      //输出字符串的容量capacity        }    }

The capacity () method represents the number of string objects in memory that can hold a string. If you want to expand memory capacity, you can use Method Ensurecapacity ().

Learn Java (ii)----The difference between a string class and a StringBuffer class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.