您的位置:首页 > 产品设计 > UI/UE

【Java学习笔记】String、StringBuffer和StringBuilder

2015-12-19 10:46 525 查看
通过查看API,对String、StringBuffer以及StringBuilder有了一定的认识,下面看API:
public final class String
extends Object
implements Serializable, Comparable<String>, CharSequence
The String 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);

公用不可修改类String继承自对象类,实现了可序列化,可比(与字符串可比),字符序列化

Java程序里的所有字符串常量,比如“abc”,都是该类的实例化。

字符串是常量。被创建后,他们的值是不可改变的。字符串缓冲区支持可变字符串,因为字符串对象不可更改它们可以被共享。

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 of characters. A string buffer is like a String, 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.


公用不可修改类StringBuffer继承自对象类,实现了可序列化,可追加,可字符序列化。

它是线程安全的可变字符序列。一个String Buffer就像一个字符串,但是,它可以被更改。在任意时间点,通过调用特定的方法,可以修改String Buffer中包含的特定字符串序列的内容和长度。

在多线程中使用String Buffer是安全的。在必要的情况下,对所有
4000
特定实例的操作都是同步的,比如每个命令的执行顺序是与那些调用他们的线程的顺序是一致的。

java.lang
Class StringBuilder

java.lang.Object
java.lang.StringBuilder
All Implemented Interfaces:
Serializable, Appendable, CharSequence

public final class StringBuilder
extends Object
implements Serializable, CharSequence
A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer 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 to StringBuffer as it will be faster under most implementations.

公用不可修改类StringBuilder继承自对象类,实现了可序列化,可追加,可字符序列化。

它是可变字符序列。该类提供了一个兼容StringBuffer的接口,但没有保证同步(个人理解为不可以在多线程中使用)。StringBuilder类被设计用来代替StringBuffer,当StringBuffer正在被被单线程使用的时候(在一般的情况)。如果可能的话,建议优先使用StringBuffer,因为StringBuffer更高效。

下面举一个小例子:

package exp;

public class StringExp {

public static void main(String[] args) {

TestPrint();

}

private static void TestPrint(){
String s;
StringBuffer sbuffer;
StringBuilder sbuilder;

s = "String_Value";
System.out.println("----------String------------");
System.out.println("初始化:"+s);
s.replaceAll("r", "+");
System.out.println("替换后:"+s);
System.out.println("\n");

System.out.println("-------StringBuffer---------");
sbuffer = new StringBuffer();
sbuffer.append("StringBuffer_Value");
System.out.println("初始化:"+sbuffer.toString());
sbuffer.replace(sbuffer.length()-6, sbuffer.length(), "TestBuffer");
System.out.println("替换后:"+sbuffer.toString());
System.out.println("\n");

System.out.println("-------StringBuilder---------");
sbuilder = new StringBuilder();
sbuilder.append("StringBuilder_Value");
System.out.println("初始化:"+sbuilder.toString());
sbuilder.replace(sbuilder.length()-6, sbuilder.length(), "TestBulder");
System.out.println("替换后:"+sbuilder.toString());

}

}

运行程序后:



【小结】
如果想输出String改变后的值,那么必须重新赋值给String对象或者重新创建新的String对象。所以,如果程序中有些字符串变量的值需要经常被更改,那么最好使用StringBuffer,因为StringBuffer不仅线程安全,而且它的内容可以实时更改。不得不说的是,StringBuffer和String的方法几乎一致,String有的方法,StringBuffer都有。StringBuffe提供的相关字符串操作方法这里就不详述了,有需要的话,查看API吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: