String Builder and String Buffer in Java
String Builder and String Buffer in Java
com/)
Strings should always be used unless string builders offer an advantage in terms of simpler code (see
the sample program at the end of this section) or better performance. For example, if you need to
concatenate a large number of strings, appending to a StringBuilder object is more efficient.
Unlike strings, every string builder also has a capacity, the number of character spaces that have been
allocated. The capacity, which is returned by the capacity() method, is always greater than or equal
to the length (usually greater than) and will automatically expand as necessary to accommodate
additions to the string builder.
StringBuilder Constructors
Constructor Description
StringBuilder()
Creates an empty string builder with a capacity of
16 (16 empty elements).
Constructs a string builder containing the same
StringBuilder(CharSequence cs)
characters as the specified CharSequence, plus an
extra 16 empty elements trailing the
CharSequence.
StringBuilder(int initCapacity)
Creates an empty string builder with the specified
initial capacity.
Creates a string builder whose value is initialized
StringBuilder(String s) by the specified string, plus an extra 16 empty
elements trailing the string.
This document is sole proprietary of Technical Lobby and any kind of misuse of this document is prohibited. This document should not be copied, modified
and re-created. Any such misuse will be entertained by Indian Copyright Law.
1
Technical Lobby, Raipur, Chhattisgarh, India (http://www.technicallobby.com/)
The StringBuilder class has some methods related to length and capacity that the String class does
not have:
A number of operations (for example, append(), insert(), or setLength()) can increase the length
of the character sequence in the string builder
so that the resultant length() would be greater than the current capacity(). When this happens, the
capacity is automatically increased.
StringBuilder Operations
The principal operations on a StringBuilder that are not available in String are the append() and
insert() methods, which are overloaded so as to accept data of any type. Each converts its argument
to a string and then appends or inserts the characters of that string to the character sequence in the
string builder. The append method always adds these characters at the end of the existing character
sequence, while the insert method adds the characters at a specified point.
This document is sole proprietary of Technical Lobby and any kind of misuse of this document is prohibited. This document should not be copied, modified
and re-created. Any such misuse will be entertained by Indian Copyright Law.
2
Technical Lobby, Raipur, Chhattisgarh, India (http://www.technicallobby.com/)
StringBuilder append(boolean b)
StringBuilder append(char c)
StringBuilder append(char[] str)
StringBuilder append(char[] str, int
offset, int len) Appends the argument to this string builder. The
StringBuilder append(double d) data is converted to a string before the append
StringBuilder append(float f) operation takes place.
StringBuilder append(int i)
StringBuilder append(long lng)
StringBuilder append(Object obj)
StringBuilder append(String s)
StringBuilder delete(int start, int end) Deletes the specified character(s) in this string
StringBuilder deleteCharAt(int index) builder.
StringBuilder insert(int offset, boolean
b)
StringBuilder insert(int offset, char c)
StringBuilder insert(int offset, char[]
str)
StringBuilder insert(int index, char[]
str, int offset, int len) Inserts the second argument into the string
StringBuilder insert(int offset, double builder. The first integer argument indicates the
d)
StringBuilder insert(int offset, float index before which the data is to be inserted. The
f) data is converted to a string before the insert
StringBuilder insert(int offset, int i) operation takes place.
StringBuilder insert(int offset, long
lng)
StringBuilder insert(int offset, Object
obj)
StringBuilder insert(int offset, String
s)
StringBuilder reverse()
Reverses the sequence of characters in this string
builder.
String toString()
Returns a string that contains the character
sequence in the builder.
This document is sole proprietary of Technical Lobby and any kind of misuse of this document is prohibited. This document should not be copied, modified
and re-created. Any such misuse will be entertained by Indian Copyright Law.
3
Technical Lobby, Raipur, Chhattisgarh, India (http://www.technicallobby.com/)
Note: You can use any String method on a StringBuilder object by first converting the string
builder to a string with the toString() method of the StringBuilder class. Then convert the string
back into a string builder using the StringBuilder(String str) constructor.
Note: There is also a StringBuffer class that is exactly the same as the StringBuilder class, except
that it is thread-safe by virtue of having its methods synchronized. Threads will be discussed in the
lesson on concurrency.
This document is sole proprietary of Technical Lobby and any kind of misuse of this document is prohibited. This document should not be copied, modified
and re-created. Any such misuse will be entertained by Indian Copyright Law.
4