Class StringBuffer
- java.lang.Object
-
- java.lang.StringBuffer
-
- All Implemented Interfaces:
- Serializable,Appendable,
CharSequence
public final class StringBuffer extends Object implements Serializable, CharSequence
A thread-safe(线程安全),mutable(可变的) sequence ofcharacters. A stringbuffer(缓冲区) is
like(如同) a
String
, but canbe modified(不可被修改). At
any(任何) point in time it containssome(一些)particular(特定) sequence ofcharacters, but the length and content of the sequence can bechanged(改变)through(通过)
certain(某些) method
calls(调用).String buffers are safe(安全) for
use(使用) by multiple threads(多线程). Themethods aresynchronized(同步的) wherenecessary(必要的)so(所以)
that all theoperations(操作)onany(任意)
particular(特定)instance(实例)behave(表现) as if they
occur(发生) in some
serial(连续的) order(顺序) that is
consistent(一致的) with theorder of the method calls made byeach(每个) of the
individual(个别的) threads
involved(涉及).The principal(最主要的)operations(操作)on a
StringBuffer
are the
append
andinsert
methods,which are overloaded(表可重载这些方法)so as toaccept(接受) data ofany type.
Each(每个)effectively(有效地)converts(转换…) agiven(给定的)datum(数据) to a string
and then appendsor inserts the characters of that string to the string buffer. Theappend
methodalways(总是) adds these characters at theend of the buffer; theinsert
method adds thecharacters at
a specified(指定) point.For example, if
z
refers to(引用) a string bufferobject whosecurrent(现在的)contents are "start
", then the method callz.append("le")
wouldcause(造成)
the stringbuffer to contain "startle
",whereas(然而)z.insert(4, "le")
would(将)
alter(改变) the string buffer to contain"starlet
".In general(通常),if sb
refers(引用) to aninstance of aStringBuffer
, thensb.append(x)
has thesame(同样的)
effect(效果) assb.insert(sb.length(), x)
.Whenever(当) anoperation(操作)occurs(出现)
involving(涉及) a source sequence (suchas appending or inserting from a source sequence) this classsynchronizes( 同步)only( 只)on the string buffer
performing(执行) theoperation, not on the source.Every string buffer has a capacity(容量).
As long as(只要) the length ofthe character sequence
contained(包含) in the string buffer doesnot
exceed(超过) thecapacity, it is not necessary(必要的) toallocate(指定) a new
internal(内部的) bufferarray. If the
internal(内部的)buffer
overflows(溢出), it isautomatically(自动)madelarger(使变大).
Asof(自……起) release JDK 5, this class
has been(已经) supplemented(补充)with an
equivalent(同意义的)class designed for use by a single thread,StringBuilder
.The
StringBuilder class should(应该)
generally(通常) be used in preference(首选项) to thisone,
as it(因为它)supports(支持) all of thesameoperations(相同操作) but it isfaster(更快的), as it performs
nosynchronization(同步).- Since:
- JDK1.0
- See Also:
StringBuilder
,String
,SerializedForm