String
public String(StringBuffer buffer)
Allocates a new string that contains the sequence of characters currently contained in the string buffer argument. The contents of the string buffer are copied; subsequent modification of the string buffer does not affect the newly created string. Parameters:
buffer - a StringBuffer.
Throws:
NullPointerException - If buffer is null.
public String(StringBuffer buffer)
Allocates a new string that contains the sequence of characters currently contained in the string buffer argument. The contents of the string buffer are copied; subsequent modification of the string buffer does not affect the newly created string. Parameters:
buffer - a StringBuffer.
Throws:
NullPointerException - If buffer is null.
String是只读字符串。StringBuffer sb = new StringBuffer("aaa");
sb.append("111"); // sb="aaa111"
String ss = sb.toString(); // ss="aaa111"
ss = ss + "222" //此时会创建一个新的内容为"aaa111222"的String,并被ss应用。
如何将StringBuffer中的东西转换成String呢?1.区别:String中的每个方法都生成新的String对象。
StringBuffer则不生成新的对象,每个方法的操作都对自己的字符序列产生影响。
2.StringBuffer里含有toString()方法,可以转换到String。
3.举例说明:
public class TestStringBuffer
{
public static void main(String[] args)
{
String a= new String("AAAA");
String b = a ;
a = a+"B";
System.out.println(a.equals(b));//输出为false
StringBuffer sa= new StringBuffer("AAAA");
StringBuffer sb = sa ;
sa.appebd("B");
System.out.println(a.equals(b));//输出为true
System.out.println(sa.toString());
}
}
用法楼上几位写的很详细了。