The difference between StringBuffer and String class. StringBuffer classes use

2010-08-20  来源:本站原创  分类:Java  人气:149 

The difference between String and StringBuffer

In short, is a relationship between variables and constants. The contents of StringBuffer objects can be modified; and String objects can not be modified once created after the re-assignment is actually two objects.

StringBuffer String internal implementations and different, StringBuffer string during processing, does not generate a new object, the memory usage is superior to the String class. So, when in actual use, if often need to modify a string, such as insert, delete operation, use StringBuffer to be more suitable for some.

String: the String class does not have to change the character string in a way, as a java string can not change one single character, it said in the JDK documentation object of String class is immutable . However, the immutable string has a considerable advantage: the compiler can be set to the shared string.

StringBuffer: StringBuffer class is an auxiliary class can be pre-allocated memory block specified length of a string buffer. This method of using the StringBuffer class append additional characters to use than the String + operator to add character to the back of an existing string is much more efficient. Use the + operator because every time the character to add to a string, the string object needs to find a new memory space to accommodate the larger string, which no condensate is a very time-consuming operation. Add more characters means that the string over and over again to re-allocate memory. Use StringBuffer class to avoid this problem.

StringBuffer is thread-safe multi-threaded program can also be very convenient to use, but the performance of programs is necessary to slightly slower, relatively speaking.

Common method of StringBuffer

StringBuffer class methods to emphasis on changes in the string such as additional, insertion and deletion, the StringBuffer and String class is the main difference.

1, append method

public StringBuffer append (boolean b)

The role of this method is appended to the end of the current StringBuffer object, similar to the connection string. After calling this method, StringBuffer object content changes, such as:

StringBuffer sb = new StringBuffer ("abc");

sb.append (true);

Sb is the value of the object will become "abctrue".

Using this method the connection string, save more than String content, such as a database SQL statement used in connection, for example:

StringBuffer sb = new StringBuffer ();

String user = "test";

String pwd = "123";

sb.append ("select * from userInfo where username =")

. Append (user)

. Append ("and pwd =")

. Append (pwd);

This value is the string object sb "select * from userInfo where username = test and pwd = 123".

2, deleteCharAt method

public StringBuffer deleteCharAt (int index)

The role of this method is to delete the specified location of the characters, then the remaining contents to form a new string. For example:

StringBuffer sb = new StringBuffer ("Test");

sb. deleteCharAt (1);

The role of the code to delete the string object sb in the index value of 1 character, that is, delete the second character, the remaining contents to form a new string. Therefore, the value of the object sb into "Tst".

There is also a feature similar to the delete method:

public StringBuffer delete (int start, int end)

The role of this method is to remove all characters within the specified range, including start, end index value does not contain the interval. For example:

StringBuffer sb = new StringBuffer ("TestString");

sb. delete (1,4);

The role of the code is to delete the index value 1 (included) to the index value of 4 (not included) all characters between the remaining characters form a new string. Sb is the value of the object is "TString".

3, insert method

public StringBuffer insert (int offset, String s)

The method is used to insert content in the StringBuffer object, and then form a new string. For example:

StringBuffer sb = new StringBuffer ("TestString");

sb.insert (4, "false");

The sample code is the role of the index value in the object sb 4 insert string false, the formation of a new string, then the execution of the object the value of sb is "TestfalseString".

4, reverse method

public StringBuffer reverse ()

The role of this method is to reverse the contents of the StringBuffer object, and then form a new string. For example:

StringBuffer sb = new StringBuffer ("abc");

sb.reverse ();

After reversal after sb content object becomes "cba".

5, setCharAt method

public void setCharAt (int index, char ch)

The role of this method is to modify the position of objects in the index value of index characters for the new character ch. For example:

StringBuffer sb = new StringBuffer ("abc");

sb.setCharAt (1, 'D');

Sb is the value of the object will become "aDc".

6, trimToSize method

public void trimToSize ()

The role of this method is to StringBuffer objects in storage space and the length of the string down to the same length, reducing waste of space.

7, constructor:

StringBuffer s0 = new StringBuffer (); allocated character buffer 16 bytes long

StringBuffer s1 = new StringBuffer (512); distribution of the 512-byte character buffer

8, for the length of the string: length ()
StringBuffer s = new StringBuffer ("www");
int i = s.length ();

returns the string part of the value of m.
substring (int start) / / return from the start after the start of the string subscript
substring (int start, int end) / / return from the start to the end-1 string

9. Replace the string
replace (int start, int end, String str)
s.replace (0,1, "qqq");

10. Converted to the same string: toString ().

相关文章
  • The difference between StringBuffer and String class. StringBuffer classes use 2010-08-20

    The difference between String and StringBuffer In short, is a relationship between variables and constants. The contents of StringBuffer objects can be modified; and String objects can not be modified once created after the re-assignment is actually

  • The difference between the three String StringBuffer StringBuilder 2010-03-29

    String is a fixed-length string, if you want to change need to re-generate new examples; String a string constant StringBuffer string variable (thread safe) StringBuilder string variable (non-thread-safe) Briefly to say, String type and StringBuffer

  • The difference between String and StringBuffer 2010-03-21

    The difference between String and StringBuffer, online information can be said to be innumerable, but when I see this article, feel inside to do very small example of representation, so turn about and make their own a little summary. In java there ar

  • String, StringBuilder, StringBuffer is the difference between 2010-08-02

    String, StringBuilder, StringBuffer is the difference between String string constants StringBuffer string variable (thread safe) StringBuilder string variable (non-thread safe) Brief says, String type and type of major performance difference between

  • JAVA in the String and StringBuffer. StringBuilder difference 2011-05-28

    http://zbj2658.blog.163.com/blog/static/400952542010080834698/ The difference between String and StringBuffer, online information can be said to be numerous, but see this article, which do feel very representative of a small example, so turn about, a

  • The efficiency difference between String and StringBuffer 2010-03-29

    We are in the encoding process, there are many string-related operations, Basically, every java programmer knows String and StringBuffer two strings above, in dealing with the efficiency of large differences In the final analysis is a String is a one

  • String and StringBuffer Xiangjie 2008-11-25

    String and StringBuffer Xiangjie (transferred) 2007-10-09 09:32 doubts about search-related information was thus found that a good text, reproduced below for reference only: String creation String s = "hello"; JVM first, according to the content

  • String and StringBuffer and StringBuilder on the understanding 2010-10-31

    Last time Hu zong (Hell, which are shielded), referring to String, StringBuffer and StringBuilder difference between. At that time the difference between the String and StringBuffer StringBuffer understanding only can set a cache, execution speed is

  • Performance comparison of StringBuffer and String 2010-11-28

    Read cherami write or use the String StringBuffer? And the following comments, mostly friends, I feel this need to get to the bottom look good, so write a simple test class and a script to run it. By modifying the test class for the different paramet

  • Detailed differences between String and StringBuffer 2010-12-17

    Detailed differences between String and StringBuffer 1. Concept 1.1, String and StringBuffer difference String: the String class does not have to change a character in the string approach, because a Java string can not change a single character, it s

  • StringBuffer and String Comparison 2008-11-23

    Original Source: http://blog.csdn.net/ybzycm/archive/2008/01/22/2058133.aspx Thank him for his conclusion in this! public class Test ( public static void stringReplace (String text) ( text = text.replace ( 'j', 'i'); ) public static void bufferReplac

  • Comparison of String and StringBuffer? 2010-04-11

    Comparison of String and StringBuffer? String: the string does not have to change the way a character string, since no single character string to change the method, said at the jdk java class object is immutable. However, advantages of immutable stri

  • String and StringBuffer 2010-08-28

    String and StringBuffer to store and operate a string that contains multiple characters character data, String class provides immutable string value, StringBuffer class provides string changes, you can use to dynamically construct StringBuffers chara

  • JAVA中StringBuffer与String的区别解析 2014-07-10

    这篇文章主要介绍了JAVA中StringBuffer与String的区别解析,需要的朋友可以参考下 看到这个讲解的不错,所以转一下 在java中有3个类来负责字符的操作. 1.Character 是进行单个字符操作的, 2.String 对一串字符进行操作,不可变类. 3.StringBuffer 也是对一串字符进行操作,是可变类. String: 是对象不是原始类型. 为不可变对象,一旦被创建,就不能修改它的值. 对于已经存在的String对象的修改都是重新创建一个新的对象,然后把新的值保存进

  • (转)StringBuilder与StringBuffer和String 的区别 2012-03-08

    很多人对String和StringBuffer的区别已经很了解了吧,可能还有人对这两个类的工作原理有些不清楚的地方,复习一下吧,顺便牵出J2SE 5.0(文档)里面带来的一个新的字符操作的类StringBuilder.那么这个StringBuilder和StringBuffer 以及我们最早遇见的 String 类有那些区别呢?在不同的场合下我们应该用哪个呢?我讲讲自己对这几个类的一点看法,也希望大家提出意见. 简要的说,String类型和StringBuffer类型的主要性能区别其实在于 St

  • String.StringBuilder.StringBuffer的区别 2013-03-03

    菜鸟的第一篇JAVA博客,现在我讲一下String. StringBuilder.StringBuffer的区别 String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) String与StringBuffer区别 简要的说, String 类型和 StringBuffer 类型的主要性能区别其实在于 String 是不可变的对象, 因此在每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象

  • 深入String 与StringBuffer详解那个更省资源 2013-09-09

    String类型,类似于常量,对它对象的每一个此操作相当于抛弃了原来的String对象,生成一个新的String对象,让对象指向新String对象地址,例如: String str1 = new String("abc");//新建一个str对象,比如为其分配地址:12345,即str指向的地址是12345 String str2 = new String("def"); str1 = str1 + str2; 解析:这里其实又新建了一个String对象其值为abc

  • JAVA in the difference between String and StringBuffer 2010-01-18

    In java there are three character classes to be responsible for the operation. 1.Character carrying out a single character to operate, 2.String right to operate a string of characters. Immutable class. 3.StringBuffer also to operate a string of chara

  • String and StringBuffer and StringBuilder difference 2010-11-09

    String string constant public final class String implements java.io.Serializable, Comparable, CharSequence StringBuffer string variable (thread safe) public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharS

  • Difference between String and StringBuffer 2010-12-20

    String class is used to create after that will not change that string, it is immutable in. The StringBuffer class is used to indicate the contents of a string variable, and provides methods to modify the underlying string. - When we spliced the chara