Java StringBuffer with StringBuilder class

When modifying a string, use the StringBuffer and StringBuilder classes.
Unlike the String class, objects of the StringBuffer and StringBuilder classes can be modified multiple times, and new unused objects are not generated.
The StringBuilder class was introduced in Java 5 and the biggest difference between it and StringBuffer is that  StringBuilder methods are not thread-safe (synchronous access is not possible).
Because StringBuilder has a speed advantage over StringBuffer, the StringBuilder class is recommended for     most situations. However, if the application requires thread safety, you must use the StringBuffer class.

Test.java file code:

public class Test{ public static void main(String args[]){ StringBuffer sBuffer = new StringBuffer("Rookie tutorial official website");
sBuffer.append("www"); sBuffer.append(".interviewbubble"); sBuffer.append(".com"); System.out.println(sBuffer); } }
The above example compiled and run results are as follows:
Rookie tutorial official website: www.interviewbubble.com

StringBuffer methods

The following are the main methods supported by the StringBuffer class:
No.Method description
1Public StringBuffer append(String s) Appends 
the specified string to this character sequence.
2Public StringBuffer reverse() 
 Replaces this character sequence with its inverted form.
3Public delete(int start, int end) 
Removes the characters in the substring of this sequence.
4public insert (int offset, int i ) 
the intstring representation of the argument into this sequence.
5replace (int start, int end, String str) 
given Stringcharacters replacing substrings of this sequence of characters.
The methods in the following list are similar to those of the String class:
No.Method description
1Int capacity() 
Returns the current capacity.
2char charAt (int index) 
Returns this sequence at the specified index charvalue.
3Void ensureCapacity(int minimumCapacity) 
Ensures that the capacity is at least equal to the specified minimum value.
4Void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 
Copies characters from this sequence to the target character array dst.
5Int indexOf(String str) 
Returns the index of the first occurrence of the specified substring in this string.
6Int indexOf(String str, int fromIndex) 
Returns the index of the first occurrence of the specified substring in this string, starting at the specified index.
7Int lastIndexOf(String str) 
Returns the index of the specified right-most substring in this string.
8Int lastIndexOf(String str, int fromIndex) 
Returns the last occurrence of the substring in the String object.
9Int length() 
 Returns the length (number of characters).
10Void setCharAt(int index, char ch) 
Sets the character at the given index to ch.
11Void setLength(int newLength) 
Sets the length of the character sequence.
12CharSequence subSequence(int start, int end) 
returns a new character sequence that is a subsequence of this sequence.
13String substring(int start) 
returns a new Stringcontaining the current subsequence of characters in this character sequence.
14String substring(int start, int end) 
returns a new Stringcontaining the current subsequence of characters in this sequence.
15String toString() 
Returns a string representation of the data in this sequence.

Comments