对String对象的任何改变都不影响到原对象,相关的任何change操作都会生成新的对象
StringBuffer是线程安全
StringBuilder是线程安全
可以修改StringBuilder和StringBuffer的内容
第1题:
String 和StringBuffer的区别
JAVA 平台提供了两个类:String 和StringBuffer,它们可以储存和操作字符串,即包含多个
字符的字符数据。这个String 类提供了数值不可改变的字符串。而这个StringBuffer 类提供
的字符串进行修改。当你知道字符数据要改变的时候你就可以使用StringBuffer。典型地,
你可以使用StringBuffers 来动态构造字符数据。另外,String 实现了equals 方法,new
String(“abc”).equals(new String(“abc”)的结果为true,而StringBuffer 没有实现equals 方法,所
以,new StringBuffer(“abc”).equals(new StringBuffer(“abc”)的结果为false。
接着要举一个具体的例子来说明,我们要把1 到100 的所有数字拼起来,组成一个串。
StringBuffer sbf = new StringBuffer();
for(int i=0;i<100;i++)
{
sbf.append(i);
}
上面的代码效率很高,因为只创建了一个StringBuffer 对象,而下面的代码效率很低,因为
创建了101 个对象。
String str = new String();
for(int i=0;i<100;i++)
{
str = str + i;
}
第2题:
String 字符串常量,StringBuffer 字符串变量(线程安全),StringBuilder 字符串变量(非线程安全)
第3题:
A.When using versions of Java technology earlier than 5.0.
B.When sharing a StringBuffer among multiple threads.
C.When using the java.io class StringBufferInputStream.
D.When you plan to reuse the StringBuffer to build more than one string.
第4题:
public class TestString3 { public static void main(String[] args) { // insert code here System.out.println(s); } } Which two code fragments, inserted independently at line 3, generate the output 4247?()
第5题:
下列关于字符串的描叙中错误的是( )
A、字符串是对象
B、String对象存储字符串的效率比StringBuffer高
C、可以使用StringBuffersb="这里是字符串"声明并初始化StringBuffer对象sb
D、String类提供了许多用来操作字符串的方法:连接,提取,查询等
第6题:
STRING和STRINGBUILDER区别
第7题:
5 string 和 stringbuffer的区别?
它们都是处理字符串的类,但是它们有一个最大的区别,那就是,String对象是存储你不能改动的文本字符
串,相反,如果你希望改动,则应使用StringBuffer类作为替换.
第8题:
String与StringBuffer的区别,以及"+"与append的区别?
第9题:
Whichtwo scenarios are NOT safe to replace a StringBuffer object with a StringBuilder object?()
第10题:
Public class test ( Public static void stringReplace (String text) ( Text = text.replace (‘j’ , ‘i’); ) public static void bufferReplace (StringBuffer text) ( text = text.append (“C”) ) public static void main (String args[]} ( String textString = new String (“java”); StringBuffer text BufferString = new StringBuffer (“java”); stringReplace (textString); BufferReplace (textBuffer); System.out.printLn (textString + textBuffer); ) ) What is the output?()