JavaSE - day10 string buffer StringBuffer

Why introduce StingBuffer?

        String is an immutable sequence of characters, which does not meet our needs, while StringBuffer is a mutable sequence of characters.
Difference between String and StringBuffer?

        1. Mutability: String is an immutable sequence of characters, while StringBuffer is a mutable sequence of characters.
        2. In memory: String will open up space in the in-memory constant pool when it is defined, which consumes more memory; StringBuffer (string buffer) will release memory after use.

       Constructor in StringBuffer: 
Constructor Summary
StringBuffer()
          Constructs a string buffer with no characters in it, with an initial capacity of 16 characters.
StringBuffer(CharSequence seq)
          public java.lang.StringBuilder(CharSequence seq) Constructs a string buffer containing the CharSequencesame .
StringBuffer(int capacity)
          Constructs a string buffer with no characters but with the specified initial capacity.
StringBuffer(String str)
          Constructs a string buffer and initializes its contents to the specified string contents.

The acquisition function of StringBuffer:


 int capacity()
          Returns the current capacity. (If the capacity is exceeded, the system will automatically allocate (when storing strings, English))
 int length()
          Returns the length (number of characters).
Examples of get functions and constructors:
public class Demo {

	public static void main(String[] args) {
		StringBuffer sb =new StringBuffer();//Create a parameterless object sb
		System.out.println("sb:"+sb+".");//There is no output address value because toString is rewritten
		
		String s = new String("Helloworld");
// StringBuffer ssb = "helloworld";//String buffer has no such way of writing
		StringBuffer sb0 = new StringBuffer("helloworld");//Construct a string buffer and initialize its content to the specified string content.
		StringBuffer sb1 = new StringBuffer(s);//Construct a string buffer and initialize it to an object s of type String, namely "Helloworld"
		System.out.println(sb0);
		System.out.println(sb1);
		
		StringBuffer sb2 = new StringBuffer(20);//The capacity becomes 50
		System.out.println("length():"+sb2.length()+", no parameter Default capacity():"+sb.capacity()+", initialized to 20 capacity():"+sb2. capacity());
		
		
		StringBuffer sb3 = new StringBuffer(sb0);//The parameter should be of type CharSequence
		System.out.println(sb0);
		
	}

}

Added features of StringBuffer

 StringBuffer append(boolean b)
          Appends the string representation of the booleanparameter to the sequence.
 StringBuffer append(char c)
          Appends the string representation of the charparameter to this sequence.
 StringBuffer append(char[] str)
          Appends the string representation of the chararray parameter to this sequence.
 StringBuffer append(char[] str, int offset, int len)
          Appends the string representation of the subarray of the chararray parameter to this sequence.
 StringBuffer append(CharSequence s)
CharSequenceAppends          the specified to the sequence.
 StringBuffer append(CharSequence s, int start, int end)
CharSequenceAppends the          specified subsequence to this sequence.
 StringBuffer append(double d)
          Appends the string representation of the doubleparameter to this sequence.
 StringBuffer append(float f)
          Appends the string representation of the floatparameter to this sequence.
 StringBuffer append(int i)
          Appends the string representation of the intparameter to this sequence.
 StringBuffer append(long lng)
          Appends the string representation of the longparameter to this sequence.
 StringBuffer append(Object obj)
          Append the string representation of the Objectparameter .
 StringBuffer append(String str)
          Appends the specified string to this sequence of characters.
 StringBuffer append(StringBuffer sb)
          Appends the specified StringBuffer to this sequence.
 StringBuffer appendCodePoint(int codePoint)
          Appends the string representation of the codePointparameter to this sequence.

The append method in the StringBuffer class seems to be able to append all types of data!
Add at specified location

StringBuffer insert(int offset, boolean b)
          Insert the string representation of the booleanparameter into this sequence.
 StringBuffer insert(int offset, char c)
          Insert the string representation of the charparameter into this sequence.
 StringBuffer insert(int offset, char[] str)
          Inserts the string representation of the chararray parameter into this sequence.
 StringBuffer insert(int index, char[] str, int offset, int len)
strInserts the string representation of a          subarray of the array parameter into this sequence.
 StringBuffer insert(int dstOffset, CharSequence s)
          Insert the specified CharSequenceinto this sequence.
 StringBuffer insert(int dstOffset, CharSequence s, int start, int end)
CharSequenceInserts the          specified subsequence into this sequence.
 StringBuffer insert(int offset, double d)
          Insert the string representation of the doubleparameter into this sequence.
 StringBuffer insert(int offset, float f)
          Insert the string representation of the floatparameter into this sequence.
 StringBuffer insert(int offset, int i)
          Insert the string representation of the intparameter into this sequence.
 StringBuffer insert(int offset, long l)
          Insert the string representation of the longparameter into this sequence.
 StringBuffer insert(int offset, Object obj)
          Inserts the string representation of the Objectparameter into this sequence of characters.
 StringBuffer insert(int offset, String str)
          Insert a string into this sequence of characters.
Like the append method, the insert method can also insert almost any type! Returns the string buffer itself.
delete function
StringBuffer delete(int start, int end)
          Removes characters from a substring of this sequence.
 StringBuffer deleteCharAt(int index)
          Removes the specified position of this sequence char.
One is to remove a string at a specified start and end, and the other is to remove a char at a specified position, both of which return StringBuffer itself.

Invert function

 StringBuffer reverse()
          将此字符序列用其反转形式取代。
   就现在的观察来看,具有字符串反转方法的只有StringBuffer,String、Character、Integer等类中都没有reverse()这个方法。所以如果需要反转功能,就必须得先变成StringBuffer类,然后才能调用反转方法。
截取功能

 String substring(int start)
          返回一个新的 String,它包含此字符序列当前所包含的字符子序列。
 String substring(int start, int end)
          返回一个新的 String,它包含此序列当前所包含的字符子序列。
截取功能同String。
替换功能

 
 
   
 StringBuffer replace(int start, int end, String str)
          使用给定 String 中的字符替换此序列的子字符串中的字符。
格式不同于String,这个我们直接在形式参数里输入替代的值,被替代部分是用start--end这个区间锁定的。



StringBuffer,String,StringBuilder的区别?

 StringBuffer和StringBuilder都是一个可变的字符序列,提供一个缓冲区.(两者都看做容器)
 StringBuffer:线程安全的,同步的,执行效率低
 StringBuilder:线程不安全的,不同步的,执行效率高,并且单线程中优先采用StringBuilder
 
 StringBuffer 执行效率虽然低,但是由于String类型,并且他可变的字符序列,提供了缓冲区

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325849946&siteId=291194637