- String是非可变类,其对象为字符串常量,不适合频繁改变,如插入(insert),删除(delete),添加(append)等。
- StringBuffer是可变类,其对象为可修改的字符序列,比较适合用来频繁的修改字符串,比如常用的是添加(append);而且是线程安全的。当需要多次修改时,尽量用这个类。
- StringBuilder是可变类,但基于单线程的,也就是说多线程是不安全的,其余和StringBuffer一样。所以速度相对较快,如果不涉及多线程,建议使用这个。
public final class Stringextends Object implements Serializable , Comparable < String >, CharSequence
TheString
class represents character strings. All string literals in Java programs, such as"abc"
, are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
String str = "abc";
is equivalent to:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
Here are some more examples of how strings can be used:
System.out.println("abc");
String cde = "cde";
System.out.println("abc" + cde);
String c = "abc".substring(2,3);
String d = cde.substring(1, 2);
The classString
includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by theCharacter
class.
The Java language provides special support for the string concatenation operator (+), and for conversion of other objects to strings.String concatenation is implemented through theStringBuilder
(orStringBuffer
) class and itsappend
method. String conversions are implemented through the methodtoString
, defined byObject
and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele,The Java Language Specification .
Unless otherwise noted, passing anull argument to a constructor or method in this class will cause aNullPointerException
to be thrown.
AString
represents a string in the UTF-16 format in whichsupplementary characters are represented bysurrogate pairs (see the sectionUnicode Character Representations in theCharacter
class for more information). Index values refer tochar
code units, so a supplementary character uses two positions in aString
.
TheString
class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e.,char
values).
-
Since:
- JDK1.0
public final class StringBufferextends Object implements Serializable , CharSequence
A thread-safe, mutable sequence of characters. A string buffer is like aString
, 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.
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.
The principal operations on aStringBuffer
are theappend
andinsert
methods, which are overloaded so as to accept data of any type.Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. Theappend
method always adds these characters at the end 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 same effect assb.insert(sb.length(),x)
.
Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence) this class synchronizes only on the string buffer performing the operation, not on the source.
Every string buffer has a capacity. 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 new internal buffer array. If the internal buffer overflows, it is automatically made larger.As of release JDK 5, this class has been supplemented (补充)with an equivalent class designed for use by a single thread,StringBuilder
.TheStringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
-
Since:
- JDK1.0
public final class StringBuilderextends Object implements Serializable , CharSequence
A mutable sequence of characters. This class provides an API compatible withStringBuffer
, but with no guarantee of synchronization.This class is designed for use as a drop-in replacement forStringBuffer
in places where the string buffer was being used by a single thread (as is generally the case).Where possible, it is recommended that this class be used in preference toStringBuffer
as it will be faster under most implementations. (faster!)
The principal operations on aStringBuilder
are theappend
andinsert
methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. Theappend
method always adds these characters at the end of the builder; theinsert
method adds the characters at a specified point.
For example, ifz
refers to a string builder object whose current contents are "start
", then the method callz.append("le")
would cause the string builder to contain "startle
", whereasz.insert(4, "le")
would alter the string builder to contain "starlet
".
In general, if sb refers to an instance of aStringBuilder
, thensb.append(x)
has the same effect assb.insert(sb.length(),x)
. Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.
Instances ofStringBuilder
are not safe for use by multiple threads. If such synchronization is required then it is recommended thatStringBuffer
be used.
-
Since:
-
1.5