String
, but can bemodified(修改). At any point in time it contains someparticular(特殊的,详细说明的)sequence of characters, but the length and content of the sequence can be changed throughcertain(某些,某几个)methodcalls(调用).
String buffers are safe for use bymultiple threads(多线程). The methods aresynchronized(同步的)wherenecessary(必要,需要)so that all theoperations(操作,运作)on any particular instance<wbr><span style="color:rgb(237,28,36)">behave(行为,<span style="color:#ED1C24; word-wrap:normal; word-break:normal; line-height:21px">表现</span><span style="color:#ED1C24; word-wrap:normal; word-break:normal; line-height:21px">)</span></span>as if(就像) they<strong><span style="color:#ED1C24; word-wrap:normal; word-break:normal; line-height:21px">occur(发生,出现)</span></strong>in<strong><span style="color:#ED1C24; word-wrap:normal; word-break:normal; line-height:21px">some serial order(串行顺序)</span></strong>that is<strong><span style="color:#ED1C24; word-wrap:normal; word-break:normal; line-height:21px">consistent(一致的)</span></strong>with the order of the method calls made by each of the<strong><span style="color:#ED1C24; word-wrap:normal; word-break:normal; line-height:21px">individual<wbr>threads(单个线程)</wbr></span></strong><wbr><strong><span style="color:#ED1C24; word-wrap:normal; word-break:normal; line-height:21px">involved(涉及的)</span></strong>.</wbr></wbr>
Theprincipal(首要的,主要的)operations on aStringBuffer
are theappend
andinsert
methods, which areoverloaded(超载的,重载)so as toaccept(接受)data of any type. Eacheffectively(有效的)convertsa given datum(一个给定的数据)to a string and then appends or inserts the characters of that string to the string buffer. Theappend
methodalways(一直,永远)adds these characters at theend(末尾)of the buffer; theinsert
method adds the characters at a specified point.
For example, ifz
refers(参考,引用)to a string buffer object whose current contents are "start
", then the method callz.append("le")
would cause(会使,会引起)the string buffer to contain "startle
", whereasz.insert(4, "le")
would alter the string buffer to contain "starlet
".
In general(通常), if sb refers to an instance of aStringBuffer
, thensb.append(x)
has the sameeffect(效果)assb.insert(sb.length(),<wbr>x)</wbr>
.
Whenever anoperation occurs(操作时)involving(涉及,包括)asource sequence(源序列)(such as appending or inserting from a source sequence) this class synchronizesonly(只有)on the string bufferperforming(执行)the operation, not on the source.
Every string buffer has acapacity(容量). As long as the length of the character sequence contained in the string buffer does not exceed(超过) the capacity, it is not necessary to allocate(分配,指定) a newinternal(内部的)buffer array. If the internal bufferoverflows(溢出,充满), it isautomatically(自动的)made larger. As ofrelease(发布,释放)JDK 5, this classhas been(已经)supplemented(被补充的,补充的)with an equivalent classdesigned(设计)for use by a single thread,StringBuilder
. TheStringBuilderclass shouldgenerally(通常,一般的)be used inpreference to this one(优先使用), as it supports all of the same operations but it is faster, as itperforms<wbr><strong><span style="color:#ED1C24; word-wrap:normal; word-break:normal; line-height:21px">no synchronization(不执行同步)</span></strong>.</wbr>