Class StringBuffer in Java

0
282
digitization, transformation, binary
Spread the love

Class StringBuffer in Java:

StringBuffer is a peer class of String. While string creates Strings of fixed length, StringBuffer creates Strings of flexible length that can be modified in terms of length and content. We can insert characters and subString in the middle of a String or append another String to the end.

A StringBuffer class implements a mutable sequence of characters. StringBuffer is safe for use by multiple threads. The methods are synchronized wherever necessary so that the operations on any particular instance behave as if they occur in some serial order. StringBuffer is used by the compiler to implement binary string concatenation (for operator +). Like-

x="a"+4+"c";
//is compiled and equivalent to
x=new StringBuffer().append("a").append(4).append("c").toString();

The StringBuffer class represents a variable-length sequence of characters. StringBuffer objects are used in computations that involve creating new String objects. The StringBuffer class provides a number of utility methods for working with StringBuffer objects, including append() and insert() methods that add characters to a StringBuffer and methods that fetch the contents of StringBuffer objects.

When a StringBuffer object is created, the constructor determines the initial contents and capacity of the StringBuffer. The capacity of a StringBuffer is the number of characters that its internal data structure can hold. This is distinct from the length of the contents of a StringBuffer, which is the number of characters that are actually stored in the StringBuffer object. The capacity of a StringBuffer can vary.

When a StringBuffer object is asked to hold more characters than its current capacity allows, the StringBuffer enlarges its internal data structure. However, it is more costly in terms of execution time and memory when a StringBuffer has to repeatedly increase its capacity than when a StringBuffer object is created with sufficient capacity.

Because the intended use of StringBuffer objects involves modifying their contents, all methods of the StringBuffer class that modify StringBuffer objects are synchronized. This means that is it safe for multiple threads to try to modify a StringBuffer object at the same time.

The principal operation on a StringBuffer are as follows:

  1. The append()-append method adds the characters at the end of the buffer
  2. The insert()- insert method adds the character at a specified point.
StringBuffer z=new StringBuffer("Love");
z.append("ly");// will give Lovely
z.insert(2,"le");//will give Loleve 

They are overloaded to accept data of any type. Each effectively converts a given data to that String to StringBuffer. Every StringBuffer has a capacity. As long as the length of the character sequence contained in the StringBuffer does not exceed the capacity, it is not necessary to allocate a new internal buffer Array. If the internal buffer overflows, it is automatically made larger.

The structure of the class StringBuffer is given as:

public class java.lang.StringBuffer extends java.lang.Object implements java.io.Serializable{
//constructors
public StringBuffer();// constructs a StringBuffer with no character.Initial capacity is 16 characters.
public StringBuffer(int length);// constructs a StringBuffer with no character.The length is specified as the length given in the argument.It can throw NegativeArraySizeException if the lenth argument is less than zero.
public StringBuffer(String str);// constructs a StringBuffer so that it represent the same sequence of characters as the String str.Initial capacity is 16 characters+length of the String str.
//Methods:
public StringBuffer append(boolean b);
public synchronized StringBuffer append(char c);
public synchronized StringBuffer append(char str[]);
public synchronized StringBuffer append(char str[],int offSet,int len);
public StringBuffer append(double d);
public StringBuffer append(float f);
public StringBuffer append(int i);
public StringBuffer append(long l);
public synchronized StringBuffer append(Object obj);
public synchronized StringBuffer append(String str);
public int capacity();
public synchronized char charAt(int index);
public synchronized void ensureCapacity(int minimumCapacity);
public synchronized void getChars(int srcBegin,int srcEnd,Char dest[],int dstBegin);
public StringBuffer insert(int offSet,boolean b);
public synchronized StringBuffer insert(int offSet,char c);
public synchronized StringBuffer insert(int offSet,char str[]);
public StringBuffer insert(int offSet,double d);
public StringBuffer insert(int offSet,float f);
public StringBuffer insert(int offSet,int i);
public StringBuffer insert(int offSet,long l);
public synchronized StringBuffer insert(int offSet,Object obj);
public synchronized StringBuffer insert(int offSet,String str);
public int length()
public StringBuffer length();
public synchronized void setCharAt(int index,char ch);
public synchronized StringBuffer reverse();
public synchronized void setLength(int newLength);
public String toString();
}

The details of the class structure are given as follows:

public StringBuffer();

public StringBuffer() constructor creates a StringBuffer object that does not contain any characters and has a capacity of 16 characters.

public StringBuffer(int length);

public StringBuffer(int length) constructor Creates a StringBuffer object that does not contain any characters and has the specified capacity.

READ  Class Locale in Java

Parameter
length – The initial capacity of this StringBuffer object

public StringBuffer(String str);

public StringBuffer(String str) constructor Creates a StringBuffer object that contains the same sequence of characters as the given String object and has a capacity 16 greater than the length of the String.

Parameter
str – A String object.

public StringBuffer append(boolean b);

public StringBuffer append(boolean b) method appends either “true” or “false” to the end of the sequence of characters stored in this StringBuffer object, depending on the value of b.

This method returns this StringBuffer object.

Parameter
b – A boolean value.

public synchronized StringBuffer append(char c);

public synchronized StringBuffer append(char c) method appends the given character to the end of the sequence of characters stored in this StringBuffer object.

This method returns this StringBuffer object.

Parameter
c – A char value.

public synchronized StringBuffer append(char str[]);

public synchronized StringBuffer append(char str[]) method appends the characters in the given array to the end of the sequence of characters stored in this StringBuffer object.

This method returns this StringBuffer object.

Parameter
str – An array of char values.

public synchronized StringBuffer append(char str[],int offSet,int len);

public synchronized StringBuffer append(char str[], int offset, int len) method appends the specified portion of the given array to the end of the character sequence stored in this StringBuffer object. The portion of the array that is appended starts offset elements from the
beginning of the array and is len elements long.

This method returns this StringBuffer object.

Parameter
str – An array of char values.
offset – An offset into the array.
len – The number of characters from the array to be appended.

public StringBuffer append(double d);

public StringBuffer append(double d) method converts the given double value to a string using Double.toString(d) and appends the resulting string to the end of the sequence of characters stored in this StringBuffer object.

This method returns this StringBuffer object.

Parameter
d – A double value.

public StringBuffer append(float f);

public StringBuffer append(float f) method converts the given float value to a string using Float.toString(f) and appends the resulting string to the end of the sequence of characters stored in this StringBuffer object.

This method returns this StringBuffer object.

Parameter
f – A float value.

public StringBuffer append(int i);

public StringBuffer append(int i) method converts the given int value to a string using Integer.toString(i) and appends the resulting string to the end of the sequence of characters stored in this StringBuffer object.

This method returns this StringBuffer object.

Parameter
i – An int value.

public StringBuffer append(long l);

public StringBuffer append(long l) method converts the given long value to a string using Long.toString(l) and appends the resulting string to the end of the sequence of characters stored in this StringBuffer object.

This method returns this StringBuffer object.

Parameter
l – A long value.

public synchronized StringBuffer append(Object obj);

public synchronized StringBuffer append(Object obj) method gets the string representation of the given object by calling String.valueOf(obj) and appends the resulting string to the end of the character sequence stored in this StringBuffer object.

This method returns this StringBuffer object.

Parameter
obj – A reference to an object.

public synchronized StringBuffer append(String str);

public synchronized StringBuffer append(String str) method appends the sequence of characters represented by the given String to the characters in this StringBuffer object. If str is null, the string “null” is appended.

This method returns this StringBuffer object.

Parameter
str – A String object.

public int capacity();

public int capacity() method returns the current capacity of this object. The capacity of a StringBuffer object is the number of characters that its internal data structure can hold. A StringBuffer object automatically increases its capacity when it is asked to hold more characters than its current capacity allows.

This method returns the capacity of this StringBuffer object.

public synchronized char charAt(int index);

public synchronized char charAt(int index) method returns the character at the specified position in the StringBuffer object. The first character in the StringBuffer is at index 0.

This method returns the character stored at the specified position in this StringBuffer object.

READ  Class FieldPosition in Java

Parameter
index – An index into the StringBuffer.

public synchronized void ensureCapacity(int minimumCapacity);

public synchronized void ensureCapacity(int minimumCapacity) method ensures that the capacity of this StringBuffer object is at least the specified number of characters. If necessary, the capacity of this object is increased to the greater of minimumCapacity or double its current capacity plus two.

It is more efficient to ensure that the capacity of a StringBuffer object is sufficient to hold all of the additions that will be made to its contents, rather than let the StringBuffer increase its capacity in multiple increments.

Parameter
minimumCapacity – The minimum desired capacity.

public synchronized void getChars(int srcBegin,int srcEnd,Char dest[],int dstBegin);

public synchronized void getChars(int srcBegin, int srcEnd, Char dest[], int dstBegin) method copies each character in the specified range of this StringBuffer object to the given array of char values. More specifically, the first character to be copied is at index srcBegin; the last character to be copied is at index srcEnd-1.

These characters are copied into dst, starting at index dstBegin and ending at index:

dstBegin + (srcEnd-srcBegin) – 1.

Parameter
srcBegin – The index of the first character to be copied.
srcEnd – The index after the last character to be copied.
dst – The destination char array.
dstBegin – An offset into the destination array.

public StringBuffer insert(int offSet,boolean b);

public StringBuffer insert(int offset, boolean b) method inserts either “true” or “false” into the sequence of characters stored in this StringBuffer object, depending on the value of b. The string is inserted at a position offset characters from the beginning of the sequence. If offset is 0, the string is inserted before the first character in the StringBuffer.

This method returns this StringBuffer object.

Parameter
offset – An offset into the StringBuffer.
b – A boolean value.

public synchronized StringBuffer insert(int offSet,char c);

public synchronized StringBuffer insert(int offset, char c) method inserts the given character into the sequence of characters stored in this StringBuffer object. The character is inserted at a position offset characters from the beginning of the sequence. If offset is 0, the character is inserted before the first character in the StringBuffer.

This method returns this StringBuffer object.

Parameter
offset – An offset into the StringBuffer.
c – A char value.

public synchronized StringBuffer insert(int offSet,char str[]);

public synchronized StringBuffer insert(int offSet,char str[]) method inserts the characters in the given array into the sequence of characters stored in this StringBuffer object. The characters are inserted at a position offset characters from the beginning of the sequence. If offset is 0, the characters are inserted before the first character in the StringBuffer.

This method returns this StringBuffer object.

Parameter
offset – An offset into the StringBuffer.
str – An array of char values.

public StringBuffer insert(int offSet,double d);

public StringBuffer insert(int offset, double d) method converts the given double value to a string using Double.toString(d) and inserts the resulting string into the sequence of characters stored in this StringBuffer object. The string is inserted at a position offset characters from the beginning of the sequence. If offset is 0, the string is inserted before the first character in the StringBuffer.

This method returns this StringBuffer object.

Parameter
offset – An offset into the StringBuffer.
d – A double value.

public StringBuffer insert(int offSet,float f);

public StringBuffer insert(int offSet,float f) method converts the given float value to a string using Float.toString(f) and inserts the resulting string into the sequence of characters stored in this StringBuffer object. The string is inserted at a position offset characters from the beginning of the sequence. If offset is 0, the string is inserted before the first character in the StringBuffer.

This method returns this StringBuffer object.

Parameter
offset – An offset into the StringBuffer.
f – A float value.

public StringBuffer insert(int offSet,int i);

public StringBuffer insert(int offSet,int i) method converts the given int value to a string using Integer.toString(i) and inserts the resulting string into the sequence of characters stored in this StringBuffer object. The string is inserted at a position offset characters from the beginning of the sequence. If offset is 0, the string is inserted before the first character in the StringBuffer

READ  Garbage Collector Simplified in Java

This method returns this StringBuffer object.

Parameter
offset – An offset into the StringBuffer.
i – An int value.

public StringBuffer insert(int offSet,long l);

public StringBuffer insert(int offset, long l) method converts the given long value to a string using Long.toString(l) and inserts the resulting string into the sequence of characters stored in this StringBuffer object. The string is inserted at a position offset characters from the beginning of the sequence. If offset is 0, the string is inserted before the first character in the StringBuffer.

This method returns this StringBuffer object.

Parameter
offset – An offset into the StringBuffer.
l – A long value.

public synchronized StringBuffer insert(int offSet,Object obj);

public synchronized StringBuffer insert(int offset, Object obj) method gets the string representation of the given object by calling String.valueOf(obj) and inserts the resulting string into the sequence of characters stored in this StringBuffer object. The string is inserted at a position offset characters from the beginning of the sequence. If offset is 0, the string is inserted before the first character in the StringBuffer.

This method returns this StringBuffer object.

Parameter
offset – An offset into the StringBuffer.
obj – A reference to an object.

public synchronized StringBuffer insert(int offSet,String str);

public synchronized StringBuffer insert(int offSet,String str) method inserts the sequence of characters represented by the given String into the sequence of characters stored in this StringBuffer object. If str is null, the string “null” is inserted. The string is inserted at a position offset characters from the beginning of the sequence. If offset is 0, the string is inserted before the first character in the StringBuffer.

This method returns this StringBuffer object.

Parameter
offset – An offset into the StringBuffer.
str – A String object.

public int length()

public int length( method returns the number of characters stored in this StringBuffer object. The length is distinct from the capacity of a StringBuffer, which is the number of characters that its internal data structure can hold.

This method returns the number of characters stored in this StringBuffer object.

public synchronized void setCharAt(int index,char ch);

public synchronized void setCharAt(int index,char ch) method modifies the character located index characters from the beginning of the sequence of characters stored in this StringBuffer object. The current character at this position is replaced by the character ch.

Parameter
index – The index of the character to be set.
ch – A char value.

public synchronized StringBuffer reverse();

public synchronized StringBuffer reverse() method reverses the sequence of characters stored in this StringBuffer object.

This method returns this StringBuffer object.

public synchronized void setLength(int newLength);

public synchronized void setLength(int newLength) method sets the length of the sequence of characters stored in this StringBuffer object. If the length is set to be less than the current length, characters are lost from the end of the character sequence. If the length is set to be more than the current length, NUL characters (\u0000) are added to the end of the character sequence.

Parameter
newLength – The new length for this StringBuffer.

public String toString();

public String toString() method returns a new String object that represents the same sequence of characters as the sequence of characters stored in this StringBuffer object. Note that any subsequent changes to the contents of this StringBuffer object do not affect the contents of the String object created by this method.

This method returns a new String object that represents the same sequence of characters as the sequence of characters stored in this StringBuffer object

Apart from these StringBuffer class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

How to convert StringBuffer to String?

After a String is processed in a StringBuffer object, it may be efficiently converted to a String object for further use. The StringBuffer.toString() method does not really copy the internal array of characters, rather it shared that array with the new String object and makes a new copy for itself. We can then further do any modifications and operations.

 


Spread the love
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

LEAVE A REPLY

Please enter your comment!
Please enter your name here