String 是定长不可变的,jdk中java.lang,String类中各属性都是final 修饰的
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence
{
/** The value is used for character storage. */
private final char value[];
/** The offset is the first index of the storage that is used. */
private final int offset;
/** The count is the number of characters in the String. */
private final int count;
所以一旦赋值之后,长度以及内容都是固定不可变了, 不要和java的引用搞混
StringBuffer 是不定长,可变,Append后内容继续增加: 继承与AbstractStringBuilder 抽象类,
abstract class AbstractStringBuilder implements Appendable, CharSequence {
/**
* The value is used for character storage.
*/
char value[];
/**
* The count is the number of characters used.
*/
int count;
StringBulier 也是不定长,内容可变与StringBuffer的区别在与StringBuffer 是线程安全的,StringBulier不是线程安全的效率比StringBuffer效率高,
jdk参考代码如下:
/*
* @(#)StringBuffer.java 1.101 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang;
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
/** use serialVersionUID from JDK 1.0.2 for interoperability */
static final long serialVersionUID = 3388685877147921107L;
/**
* Constructs a string buffer with no characters in it and an
* initial capacity of 16 characters.
*/
public StringBuffer() {
super(16);
}
/**
* Constructs a string buffer with no characters in it and
* the specified initial capacity.
*
* @param capacity the initial capacity.
* @exception NegativeArraySizeException if the <code>capacity</code>
* argument is less than <code>0</code>.
*/
public StringBuffer(int capacity) {
super(capacity);
}
/**
* Constructs a string buffer initialized to the contents of the
* specified string. The initial capacity of the string buffer is
* <code>16</code> plus the length of the string argument.
*
* @param str the initial contents of the buffer.
* @exception NullPointerException if <code>str</code> is <code>null</code>
*/
public StringBuffer(String str) {
super(str.length() + 16);
append(str);
}
/**
* Constructs a string buffer that contains the same characters
* as the specified <code>CharSequence</code>. The initial capacity of
* the string buffer is <code>16</code> plus the length of the
* <code>CharSequence</code> argument.
* <p>
* If the length of the specified <code>CharSequence</code> is
* less than or equal to zero, then an empty buffer of capacity
* <code>16</code> is returned.
*
* @param seq the sequence to copy.
* @exception NullPointerException if <code>seq</code> is <code>null</code>
* @since 1.5
*/
public StringBuffer(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}
public synchronized int length() {
return count;
}
public synchronized int capacity() {
return value.length;
}
public synchronized void ensureCapacity(int minimumCapacity) {
if (minimumCapacity > value.length) {
expandCapacity(minimumCapacity);
}
}
/**
* @since 1.5
*/
public synchronized void trimToSize() {
super.trimToSize();
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @see #length()
*/
public synchronized void setLength(int newLength) {
super.setLength(newLength);
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @see #length()
*/
public synchronized char charAt(int index) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
return value[index];
}
/**
* @since 1.5
*/
public synchronized int codePointAt(int index) {
return super.codePointAt(index);
}
/**
* @since 1.5
*/
public synchronized int codePointBefore(int index) {
return super.codePointBefore(index);
}
/**
* @since 1.5
*/
public synchronized int codePointCount(int beginIndex, int endIndex) {
return super.codePointCount(beginIndex, endIndex);
}
/**
* @since 1.5
*/
public synchronized int offsetByCodePoints(int index, int codePointOffset) {
return super.offsetByCodePoints(index, codePointOffset);
}
/**
* @throws NullPointerException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public synchronized void getChars(int srcBegin, int srcEnd, char dst[],
int dstBegin)
{
super.getChars(srcBegin, srcEnd, dst, dstBegin);
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @see #length()
*/
public synchronized void setCharAt(int index, char ch) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
value[index] = ch;
}
/**
* @see java.lang.String#valueOf(java.lang.Object)
* @see #append(java.lang.String)
*/
public synchronized StringBuffer append(Object obj) {
super.append(String.valueOf(obj));
return this;
}
public synchronized StringBuffer append(String str) {
super.append(str);
return this;
}
public synchronized StringBuffer append(StringBuffer sb) {
super.append(sb);
return this;
}
public StringBuffer append(CharSequence s) {
// Note, synchronization achieved via other invocations
if (s == null)
s = "null";
if (s instanceof String)
return this.append((String)s);
if (s instanceof StringBuffer)
return this.append((StringBuffer)s);
return this.append(s, 0, s.length());
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @since 1.5
*/
public synchronized StringBuffer append(CharSequence s, int start, int end)
{
super.append(s, start, end);
return this;
}
public synchronized StringBuffer append(char str[]) {
super.append(str);
return this;
}
public synchronized StringBuffer append(char str[], int offset, int len) {
super.append(str, offset, len);
return this;
}
/**
* @see java.lang.String#valueOf(boolean)
* @see #append(java.lang.String)
*/
public synchronized StringBuffer append(boolean b) {
super.append(b);
return this;
}
public synchronized StringBuffer append(char c) {
super.append(c);
return this;
}
/**
* @see java.lang.String#valueOf(int)
* @see #append(java.lang.String)
*/
public synchronized StringBuffer append(int i) {
super.append(i);
return this;
}
/**
* @since 1.5
*/
public synchronized StringBuffer appendCodePoint(int codePoint) {
super.appendCodePoint(codePoint);
return this;
}
/**
* @see java.lang.String#valueOf(long)
* @see #append(java.lang.String)
*/
public synchronized StringBuffer append(long lng) {
super.append(lng);
return this;
}
/**
* @see java.lang.String#valueOf(float)
* @see #append(java.lang.String)
*/
public synchronized StringBuffer append(float f) {
super.append(f);
return this;
}
/**
* @see java.lang.String#valueOf(double)
* @see #append(java.lang.String)
*/
public synchronized StringBuffer append(double d) {
super.append(d);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @since 1.2
*/
public synchronized StringBuffer delete(int start, int end) {
super.delete(start, end);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @since 1.2
*/
public synchronized StringBuffer deleteCharAt(int index) {
super.deleteCharAt(index);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @since 1.2
*/
public synchronized StringBuffer replace(int start, int end, String str) {
super.replace(start, end, str);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @since 1.2
*/
public synchronized String substring(int start) {
return substring(start, count);
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @since 1.4
*/
public synchronized CharSequence subSequence(int start, int end) {
return super.substring(start, end);
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @since 1.2
*/
public synchronized String substring(int start, int end) {
return super.substring(start, end);
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @since 1.2
*/
public synchronized StringBuffer insert(int index, char str[], int offset,
int len)
{
super.insert(index, str, offset, len);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see java.lang.String#valueOf(java.lang.Object)
* @see #insert(int, java.lang.String)
* @see #length()
*/
public synchronized StringBuffer insert(int offset, Object obj) {
super.insert(offset, String.valueOf(obj));
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see #length()
*/
public synchronized StringBuffer insert(int offset, String str) {
super.insert(offset, str);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
public synchronized StringBuffer insert(int offset, char str[]) {
super.insert(offset, str);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @since 1.5
*/
public StringBuffer insert(int dstOffset, CharSequence s) {
// Note, synchronization achieved via other invocations
if (s == null)
s = "null";
if (s instanceof String)
return this.insert(dstOffset, (String)s);
return this.insert(dstOffset, s, 0, s.length());
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @since 1.5
*/
public synchronized StringBuffer insert(int dstOffset, CharSequence s,
int start, int end)
{
super.insert(dstOffset, s, start, end);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see java.lang.String#valueOf(boolean)
* @see #insert(int, java.lang.String)
* @see #length()
*/
public StringBuffer insert(int offset, boolean b) {
return insert(offset, String.valueOf(b));
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @see #length()
*/
public synchronized StringBuffer insert(int offset, char c) {
super.insert(offset, c);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see java.lang.String#valueOf(int)
* @see #insert(int, java.lang.String)
* @see #length()
*/
public StringBuffer insert(int offset, int i) {
return insert(offset, String.valueOf(i));
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see java.lang.String#valueOf(long)
* @see #insert(int, java.lang.String)
* @see #length()
*/
public StringBuffer insert(int offset, long l) {
return insert(offset, String.valueOf(l));
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see java.lang.String#valueOf(float)
* @see #insert(int, java.lang.String)
* @see #length()
*/
public StringBuffer insert(int offset, float f) {
return insert(offset, String.valueOf(f));
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see java.lang.String#valueOf(double)
* @see #insert(int, java.lang.String)
* @see #length()
*/
public StringBuffer insert(int offset, double d) {
return insert(offset, String.valueOf(d));
}
/**
* @throws NullPointerException {@inheritDoc}
* @since 1.4
*/
public int indexOf(String str) {
return indexOf(str, 0);
}
/**
* @throws NullPointerException {@inheritDoc}
* @since 1.4
*/
public synchronized int indexOf(String str, int fromIndex) {
return String.indexOf(value, 0, count,
str.toCharArray(), 0, str.length(), fromIndex);
}
/**
* @throws NullPointerException {@inheritDoc}
* @since 1.4
*/
public int lastIndexOf(String str) {
// Note, synchronization achieved via other invocations
return lastIndexOf(str, count);
}
/**
* @throws NullPointerException {@inheritDoc}
* @since 1.4
*/
public synchronized int lastIndexOf(String str, int fromIndex) {
return String.lastIndexOf(value, 0, count,
str.toCharArray(), 0, str.length(), fromIndex);
}
/**
* @since JDK1.0.2
*/
public synchronized StringBuffer reverse() {
super.reverse();
return this;
}
public synchronized String toString() {
return new String(value, 0, count);
}
/**
* Serializable fields for StringBuffer.
*
* @serialField value char[]
* The backing character array of this StringBuffer.
* @serialField count int
* The number of characters in this StringBuffer.
* @serialField shared boolean
* A flag indicating whether the backing array is shared.
* The value is ignored upon deserialization.
*/
private static final java.io.ObjectStreamField[] serialPersistentFields =
{
new java.io.ObjectStreamField("value", char[].class),
new java.io.ObjectStreamField("count", Integer.TYPE),
new java.io.ObjectStreamField("shared", Boolean.TYPE),
};
/**
* readObject is called to restore the state of the StringBuffer from
* a stream.
*/
private synchronized void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
java.io.ObjectOutputStream.PutField fields = s.putFields();
fields.put("value", value);
fields.put("count", count);
fields.put("shared", false);
s.writeFields();
}
/**
* readObject is called to restore the state of the StringBuffer from
* a stream.
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
java.io.ObjectInputStream.GetField fields = s.readFields();
value = (char[])fields.get("value", null);
count = (int)fields.get("count", 0);
}
}
/*
* @(#)StringBuilder.java 1.11 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang;
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
/** use serialVersionUID for interoperability */
static final long serialVersionUID = 4383685877147921099L;
/**
* Constructs a string builder with no characters in it and an
* initial capacity of 16 characters.
*/
public StringBuilder() {
super(16);
}
/**
* Constructs a string builder with no characters in it and an
* initial capacity specified by the <code>capacity</code> argument.
*
* @param capacity the initial capacity.
* @throws NegativeArraySizeException if the <code>capacity</code>
* argument is less than <code>0</code>.
*/
public StringBuilder(int capacity) {
super(capacity);
}
/**
* Constructs a string builder initialized to the contents of the
* specified string. The initial capacity of the string builder is
* <code>16</code> plus the length of the string argument.
*
* @param str the initial contents of the buffer.
* @throws NullPointerException if <code>str</code> is <code>null</code>
*/
public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}
/**
* Constructs a string builder that contains the same characters
* as the specified <code>CharSequence</code>. The initial capacity of
* the string builder is <code>16</code> plus the length of the
* <code>CharSequence</code> argument.
*
* @param seq the sequence to copy.
* @throws NullPointerException if <code>seq</code> is <code>null</code>
*/
public StringBuilder(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}
/**
* @see java.lang.String#valueOf(java.lang.Object)
* @see #append(java.lang.String)
*/
public StringBuilder append(Object obj) {
return append(String.valueOf(obj));
}
public StringBuilder append(String str) {
super.append(str);
return this;
}
// Appends the specified string builder to this sequence.
private StringBuilder append(StringBuilder sb) {
if (sb == null)
return append("null");
int len = sb.length();
int newcount = count + len;
if (newcount > value.length)
expandCapacity(newcount);
sb.getChars(0, len, value, count);
count = newcount;
return this;
}
/**
* Appends the specified <tt>StringBuffer</tt> to this sequence.
* <p>
* The characters of the <tt>StringBuffer</tt> argument are appended,
* in order, to this sequence, increasing the
* length of this sequence by the length of the argument.
* If <tt>sb</tt> is <tt>null</tt>, then the four characters
* <tt>"null"</tt> are appended to this sequence.
* <p>
* Let <i>n</i> be the length of this character sequence just prior to
* execution of the <tt>append</tt> method. Then the character at index
* <i>k</i> in the new character sequence is equal to the character at
* index <i>k</i> in the old character sequence, if <i>k</i> is less than
* <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i>
* in the argument <code>sb</code>.
*
* @param sb the <tt>StringBuffer</tt> to append.
* @return a reference to this object.
*/
public StringBuilder append(StringBuffer sb) {
super.append(sb);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public StringBuilder append(CharSequence s) {
if (s == null)
s = "null";
if (s instanceof String)
return this.append((String)s);
if (s instanceof StringBuffer)
return this.append((StringBuffer)s);
if (s instanceof StringBuilder)
return this.append((StringBuilder)s);
return this.append(s, 0, s.length());
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public StringBuilder append(CharSequence s, int start, int end) {
super.append(s, start, end);
return this;
}
public StringBuilder append(char str[]) {
super.append(str);
return this;
}
public StringBuilder append(char str[], int offset, int len) {
super.append(str, offset, len);
return this;
}
/**
* @see java.lang.String#valueOf(boolean)
* @see #append(java.lang.String)
*/
public StringBuilder append(boolean b) {
super.append(b);
return this;
}
public StringBuilder append(char c) {
super.append(c);
return this;
}
/**
* @see java.lang.String#valueOf(int)
* @see #append(java.lang.String)
*/
public StringBuilder append(int i) {
super.append(i);
return this;
}
/**
* @see java.lang.String#valueOf(long)
* @see #append(java.lang.String)
*/
public StringBuilder append(long lng) {
super.append(lng);
return this;
}
/**
* @see java.lang.String#valueOf(float)
* @see #append(java.lang.String)
*/
public StringBuilder append(float f) {
super.append(f);
return this;
}
/**
* @see java.lang.String#valueOf(double)
* @see #append(java.lang.String)
*/
public StringBuilder append(double d) {
super.append(d);
return this;
}
/**
* @since 1.5
*/
public StringBuilder appendCodePoint(int codePoint) {
super.appendCodePoint(codePoint);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
public StringBuilder delete(int start, int end) {
super.delete(start, end);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
public StringBuilder deleteCharAt(int index) {
super.deleteCharAt(index);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
public StringBuilder replace(int start, int end, String str) {
super.replace(start, end, str);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
public StringBuilder insert(int index, char str[], int offset,
int len)
{
super.insert(index, str, offset, len);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see java.lang.String#valueOf(java.lang.Object)
* @see #insert(int, java.lang.String)
* @see #length()
*/
public StringBuilder insert(int offset, Object obj) {
return insert(offset, String.valueOf(obj));
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see #length()
*/
public StringBuilder insert(int offset, String str) {
super.insert(offset, str);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
public StringBuilder insert(int offset, char str[]) {
super.insert(offset, str);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public StringBuilder insert(int dstOffset, CharSequence s) {
if (s == null)
s = "null";
if (s instanceof String)
return this.insert(dstOffset, (String)s);
return this.insert(dstOffset, s, 0, s.length());
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public StringBuilder insert(int dstOffset, CharSequence s,
int start, int end)
{
super.insert(dstOffset, s, start, end);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see java.lang.String#valueOf(boolean)
* @see #insert(int, java.lang.String)
* @see #length()
*/
public StringBuilder insert(int offset, boolean b) {
super.insert(offset, b);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
* @see #length()
*/
public StringBuilder insert(int offset, char c) {
super.insert(offset, c);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see java.lang.String#valueOf(int)
* @see #insert(int, java.lang.String)
* @see #length()
*/
public StringBuilder insert(int offset, int i) {
return insert(offset, String.valueOf(i));
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see java.lang.String#valueOf(long)
* @see #insert(int, java.lang.String)
* @see #length()
*/
public StringBuilder insert(int offset, long l) {
return insert(offset, String.valueOf(l));
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see java.lang.String#valueOf(float)
* @see #insert(int, java.lang.String)
* @see #length()
*/
public StringBuilder insert(int offset, float f) {
return insert(offset, String.valueOf(f));
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
* @see java.lang.String#valueOf(double)
* @see #insert(int, java.lang.String)
* @see #length()
*/
public StringBuilder insert(int offset, double d) {
return insert(offset, String.valueOf(d));
}
/**
* @throws NullPointerException {@inheritDoc}
*/
public int indexOf(String str) {
return indexOf(str, 0);
}
/**
* @throws NullPointerException {@inheritDoc}
*/
public int indexOf(String str, int fromIndex) {
return String.indexOf(value, 0, count,
str.toCharArray(), 0, str.length(), fromIndex);
}
/**
* @throws NullPointerException {@inheritDoc}
*/
public int lastIndexOf(String str) {
return lastIndexOf(str, count);
}
/**
* @throws NullPointerException {@inheritDoc}
*/
public int lastIndexOf(String str, int fromIndex) {
return String.lastIndexOf(value, 0, count,
str.toCharArray(), 0, str.length(), fromIndex);
}
public StringBuilder reverse() {
super.reverse();
return this;
}
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
}
/**
* Save the state of the <tt>StringBuilder</tt> instance to a stream
* (that is, serialize it).
*
* @serialData the number of characters currently stored in the string
* builder (<tt>int</tt>), followed by the characters in the
* string builder (<tt>char[]</tt>). The length of the
* <tt>char</tt> array may be greater than the number of
* characters currently stored in the string builder, in which
* case extra characters are ignored.
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
s.defaultWriteObject();
s.writeInt(count);
s.writeObject(value);
}
/**
* readObject is called to restore the state of the StringBuffer from
* a stream.
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
count = s.readInt();
value = (char[]) s.readObject();
}
}