Java Reference
In-Depth Information
You can use objects of the
StringBuilder
class, instead of the
String
class, in situations where content of a string
changes frequently. Recall that because of the immutability of the
String
class, string manipulations using a
String
object result in many new
String
objects, which in turn degrade the performance. A
StringBuilder
object can be
thought of as a modifiable string. It has many methods to modify its contents.
The
StringBuilder
class contains four constructors:
•
StringBuilder()
•
StringBuilder(CharSequence seq)
•
StringBuilder(int capacity)
•
StringBuilder(String str)
The no-args constructor creates an empty
StringBuilder
with a default capacity of 16.
The second constructor takes a
CharSequence
object as an argument. It creates a
StringBuilder
object, whose
content is the same as the specified
CharSequence
.
The third constructor takes an
int
as argument; it creates an empty
StringBuilder
object whose initial capacity
is the same as the specified argument. The capacity of a
StringBuilder
is the number of characters it can hold
without allocating more space. The capacity gets adjusted automatically when additional space is needed.
The fourth constructor takes a
String
and creates a
StringBuilder
that has the same content as the specified
String
. The following are some examples of creating
StringBuilder
objects:
// Create an empty StringBuilder with a default initial capacity of 16 characters
StringBuilder sb1 = new StringBuilder();
// Create a StringBuilder from of a string
StringBuilder sb2 = new StringBuilder("Here is the content");
// Create an empty StringBuilder with 200 characters as the initial capacity
StringBuilder sb3 = new StringBuilder(200);
The
append()
method lets you add text to the end of the
StringBuilder
. It is overloaded. It takes many types
of arguments. Please refer to the API documentation for the class for the complete list of all overloaded
append()
methods. It has other methods, for example,
insert()
and
delete()
, that let you modify its content, too.
The
StringBuilder
class has two properties:
length
and
capacity
. At a given point in time, their values may
not be the same. Its length refers to the length of its content whereas its capacity refers to the maximum number of
characters it can hold without going for new memory to be allocated. Its length can be, at most, equal to its capacity.
The
length()
and
capacity()
methods return its length and capacity, respectively. For example,
StringBuilder sb = new StringBuilder(200); // Capacity:200, length:0
sb.append("Hello"); // Capacity:200, length:5
int len = sb.length(); // len is assigned 5
int capacity = sb.capacity(); // capacity is assigned 200
Capacity of a
StringBuilder
is controlled by the runtime, whereas its length is controlled by the content you
place in it. The runtime adjusts the capacity as its content is modified.
You can get the content of a
StringBuilder
as a
String
by using its
toString()
method.
// Create a String object
String s1 = new String("Hello");
// Create a StringBuilder from of the String object s1
StringBuilder sb = new StringBuilder(s1);