Java Reference
In-Depth Information
String
objects cannot be changed, but you have been creating strings that are combinations and modifica-
tions of existing
String
objects, so how is this done? Java has two other standard classes that encapsulate
strings, the
StringBuffer
class and the
StringBuilder
class, and both
StringBuffer
and
StringBuild-
er
objects can be altered directly. Strings that can be changed are referred to as
mutable strings
, in contrast
to
String
objects that are
immutable strings
. Java uses objects of the
StringBuffer
class type internally to
perform many of the operations that involve combining
String
objects. After the required string has been
formed as a
StringBuffer
object, it is then converted to an object of type
String
.
You have the choice of using either a
StringBuffer
object or a
StringBuilder
object whenever you
need a string that you can change directly, so what's the difference? In terms of the operations these two
classes provide, there is no difference, but
StringBuffer
objects are safe for use by multiple threads, where-
as
StringBuilder
objects are not. You learn about threads in Chapter 16, but in case you're not familiar
with the term,
threads
are just independent execution processes within a program that can execute concur-
rently. For example, an application that involves acquiring data from several remote sites could implement
the data transfer from each remote site as a separate thread. This would allow these relatively slow opera-
tions to execute in parallel, sharing processor time in a manner determined by the operating system. This
usually means that the elapsed time for acquiring all the data from the remote sites is much less than if the
operations were executed sequentially in a single thread of execution.
Of course, if concurrent threads of execution access the same object, there is potential for problems.
Complications can arise when one thread might be accessing an object while another is in the process of
modifying it. When this sort of thing is possible in your application, you must use the
StringBuffer
class
to define mutable strings if you want to avoid trouble. The
StringBuffer
class operations have been coded
to prevent errors arising from concurrent access by two or more threads. If you are sure that your mutable
strings will be accessed only by a single thread of execution, then you should use
StringBuilder
objects
because operations on these will be faster than with
StringBuffer
objects.
So when should you use mutable
String
objects rather than immutable
String
objects?
StringBuffer
and
StringBuilder
objects come into their own when you are transforming strings frequently — adding,
deleting, or replacing substrings in a string. Operations are faster and easier using mutable objects. If you
have mainly static strings that you occasionally need to concatenate in your application, then
String
objects
are the best choice. Of course, if you want to, you can mix the use of both mutable and immutable in the
same program.
As I said, the
StringBuilder
class provides the same set of operations as the
StringBuffer
class. I de-
scribe mutable string operations in terms of the
StringBuffer
class for the rest of this chapter because this
is always a safe choice, but don't forget that all the operations that I discuss in the context of
StringBuffer
are available with the
StringBuilder
class, which is faster but not thread-safe.
Creating StringBuffer Objects
You can create a
StringBuffer
object that contains a given string with the following statement:
StringBuffer aString = new StringBuffer("A stitch in time");
This declares a
StringBuffer
object,
aString
, and initializes it with the string
"A stitch in time"
.
When you are initializing a
StringBuffer
object, you must use this syntax, with the keyword
new
, the
StringBuffer
class name, and the initializing value between parentheses. You cannot just use the string as
the initializing value as you did with
String
objects. This is because there is rather more to a
StringBuffer