📄 javalang.doc12.html
字号:
<html>
<head>
<title>The Java Language Specification The Package java.lang </title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="javalang.doc11.html">Prev</a> | <a href="javalang.doc13.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="14461"></a>
<center><h1>20.13 The Class <code>java.lang.StringBuffer</code></h1></center>
<a name="2616"></a>
A string buffer is like a <code>String</code> <a href="javalang.doc11.html#14460">(§20.12)</a>, but can be modified. At any point in
time it contains some particular sequence of characters, but the length and content
of the sequence can be changed through certain method calls.
<p><pre><a name="14027"></a>public class <code><b>StringBuffer</b></code> {
<a name="2985"></a> public <code><b>StringBuffer</b></code>();
<a name="2986"></a> public <code><b>StringBuffer</b></code>(int length)<br>
throws NegativeArraySizeException;
<a name="2987"></a> public <code><b>StringBuffer</b></code>(String str);
<a name="2988"></a> public String <code><b>toString</b></code>();
<a name="2989"></a> public int <code><b>length</b></code>();
<a name="3023"></a> public void <code><b>setLength</b></code>(int newLength)
<a name="5141"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="2990"></a> public int <code><b>capacity</b></code>();
<a name="2991"></a> public void <code><b>ensureCapacity</b></code>(int minimumCapacity);
<a name="2993"></a> public char <code><b>charAt</b></code>(int index)
<a name="5136"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="5680"></a> public void <code><b>setCharAt</b></code>(int index, char ch)
<a name="5681"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="2994"></a> public void <code><b>getChars</b></code>(int srcBegin, int srcEnd,
<a name="5129"></a> char[] dst, int dstBegin)<br>
throws NullPointerException, <code>IndexOutOfBoundsException</code>;
<a name="2996"></a> public StringBuffer <code><b>append</b></code>(Object obj);
<a name="2997"></a> public StringBuffer <code><b>append</b></code>(String str);
<a name="2998"></a> public StringBuffer <code><b>append</b></code>(char[] str)
<a name="25589"></a> throws NullPointerException;
<a name="2999"></a> public StringBuffer <code><b>append</b></code>(char[] str, int offset, int len)
<a name="25572"></a> throws NullPointerException, <code>IndexOutOfBoundsException</code>;
<a name="3000"></a> public StringBuffer <code><b>append</b></code>(boolean b);
<a name="3001"></a> public StringBuffer <code><b>append</b></code>(char c);
<a name="3002"></a> public StringBuffer <code><b>append</b></code>(int i);
<a name="3003"></a> public StringBuffer <code><b>append</b></code>(long l);
<a name="3004"></a> public StringBuffer <code><b>append</b></code>(float f);
<a name="3005"></a> public StringBuffer <code><b>append</b></code>(double d);
<a name="5147"></a> public StringBuffer <code><b>insert</b></code>(int offset, Object obj)
<a name="3006"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="5152"></a> public StringBuffer <code><b>insert</b></code>(int offset, String str)
<a name="3007"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="5157"></a> public StringBuffer <code><b>insert</b></code>(int offset, char[] str)
<a name="3008"></a> throws NullPointerException, <code>IndexOutOfBoundsException</code>;
<a name="5162"></a> public StringBuffer <code><b>insert</b></code>(int offset, boolean b)
<a name="3009"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="5167"></a> public StringBuffer <code><b>insert</b></code>(int offset, char c)
<a name="3010"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="5172"></a> public StringBuffer <code><b>insert</b></code>(int offset, int i)
<a name="3011"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="5177"></a> public StringBuffer <code><b>insert</b></code>(int offset, long l)
<a name="3012"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="5182"></a> public StringBuffer <code><b>insert</b></code>(int offset, float f)
<a name="3013"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="5187"></a> public StringBuffer <code><b>insert</b></code>(int offset, double d)
<a name="3014"></a> throws <code>IndexOutOfBoundsException</code>;
<a name="30697"></a> public StringBuffer <code><b>reverse</b></code>();
<a name="30699"></a>}
</pre><a name="4964"></a>
A string buffer has a <i>capacity</i>. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to create a new internal buffer array.<p>
<a name="5017"></a>
String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.<p>
<a name="4965"></a>
String buffers can be used by a compiler to implement the binary string concatenation operator <code>+</code> <a href="15.doc.html#39990">(§15.17.1)</a>. For example, suppose <code>k</code> has type <code>int</code> and <code>a</code> has type <code>Object</code>. Then the expression:<p>
<pre><a name="4966"></a><code>k + "/" + a
</code></pre><a name="4967"></a>
can be compiled as if it were the expression:<p>
<pre><a name="4968"></a>new StringBuffer().append(k).append("/").<br>
append(a).toString()
</pre><a name="4975"></a>
which creates a new string buffer (initially empty), appends the string representation of each operand to the string buffer in turn, and then converts the contents of the string buffer to a string. Overall, this avoids creating many temporary strings.<p>
<a name="5233"></a>
The principal operations on a <code>StringBuffer</code> are the <code>append</code> and <code>insert</code> methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then adds the characters of that string to the contents of the string buffer. The <code>append</code> method always adds these characters at the end of the buffer; the <code>insert</code> method adds the characters at a specified point.<p>
<a name="30693"></a>
For example, if <code>z</code> refers to a string buffer object whose current contents are the characters "<code>start</code>", then the method call <code>z.append("le")</code> would alter the string buffer to contain the characters "<code>startle</code>", but <code>z.insert(4, "le")</code> would alter the string buffer to contain the characters "<code>starlet</code>".<p>
<a name="5240"></a>
In general, if <code>sb</code> refers to an instance of a <code>StringBuffer</code>, then <code>sb.append(x)</code> has the same effect as <code>sb.insert(sb.length(), x)</code>.<p>
<a name="14028"></a>
<p><font size=+1><strong>20.13.1 </strong> <code>public <code><b>StringBuffer</b></code>()</code></font>
<p>
<a name="5006"></a>
This constructor initializes a newly created <code>StringBuffer</code> object so that it initially
represents an empty character sequence and has capacity 16.
<p><a name="14029"></a>
<p><font size=+1><strong>20.13.2 </strong> <code>public <code><b>StringBuffer</b></code>(int length)<br>throws NegativeArraySizeException</code></font>
<p>
<a name="5019"></a>
This constructor initializes a newly created <code>StringBuffer</code> object so that it initially
represents an empty character sequence, but has the capacity specified by
the argument.
<p><a name="5031"></a>
If the argument is negative, a <code>NegativeArraySizeException</code> is thrown.<p>
<a name="14030"></a>
<p><font size=+1><strong>20.13.3 </strong> <code>public <code><b>StringBuffer</b></code>(String str)</code></font>
<p>
<a name="5010"></a>
This constructor initializes a newly created <code>StringBuffer</code> object so that it represents
the same sequence of characters as the argument; in other words, the initial
contents of the string buffer is a copy of the argument string. The initial capacity
of the string buffer is 16 plus the length of the argument string.
<p><a name="2620"></a>
<p><font size=+1><strong>20.13.4 </strong> <code>public String <code><b>toString</b></code>()</code></font>
<p>
<a name="5039"></a>
A new <code>String</code> object is created and initialized to contain the character sequence
currently represented by the string buffer; the new <code>String</code> is then returned. Any
subsequent changes to the string buffer do not affect the contents of the returned
string.
<p><a name="5043"></a>
Implementation advice: This method can be coded so as to create a new <code>String</code> object without allocating new memory to hold a copy of the character sequence. Instead, the string can share the memory used by the string buffer. Any subsequent operation that alters the content or capacity of the string buffer must then make a copy of the internal buffer at that time. This strategy is effective for reducing the amount of memory allocated by a string concatenation operation <a href="15.doc.html#39990">(§15.17.1)</a> when it is implemented using a string buffer.<p>
<a name="5050"></a>
Overrides the <code>toString</code> method of <code>Object</code> <a href="javalang.doc1.html#1152">(§20.1.2)</a>.<p>
<a name="14031"></a>
<p><font size=+1><strong>20.13.5 </strong> <code>public int <code><b>length</b></code>()</code></font>
<p>
<a name="5054"></a>
This method returns the length of the sequence of characters currently represented
by this <code>StringBuffer</code> object.
<p><a name="14032"></a>
<p><font size=+1><strong>20.13.6 </strong> <code>public int <code><b>capacity</b></code>()</code></font>
<p>
<a name="5057"></a>
The current capacity of this <code>StringBuffer</code> object is returned.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -