This code is NOT thread-safe.
The programmer can replace StringBuffer with StringBuilder with no other changes.
This code will perform well and converting the code to use StringBuilder will not enhance the performance.
This code will perform poorly. For better performance, the code should be rewritten: return “<“+ this.name + “>”;
第1题:
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.
第2题:
执行下列程序后,输出结果为( )。 public class Test { public static void main (String[] args) { StringBuffer sb = new StringBuffer("北京 2008" ); System. out. println ("length =" + sb. length ( ) ); } }
A.length = 8
B.length = 10
C.length = 6
D.length = 20
第3题:
( 20 )请阅读下面程序
public class ExampleStringBuffer{
public static void main ( String[] args ){
StringBuffer sb=new StringBuffer ( "test" ) ;
System.out.println ( "buffer= "+sb ) ;
System.out.println ( "length= "+sb.length ()) ; }}
程序运行结果中在 "length=" 后输出的值是
A ) 10
B ) 4
C ) 20
D ) 30
第4题:
Whichtwo scenarios are NOT safe to replace a StringBuffer object with a StringBuilder object?()
第5题:
第6题:
A.Foo { public int bar() { return 1; }
B.new Foo { public int bar() { return 1; }
C.new Foo() { public int bar() { return 1; }
D.new class Foo { public int bar() { return 1; }
第7题:
阅读以下说明和Java代码,回答问题
[说明]
在某些系统中,存在非常复杂的对象,可以采用循序渐进的方式进行组合将小对象组合,成复杂的对象。
以下实例展示了Builder(生成器)模式。该实例用来建立“文件”,文件内容包括:一个标题、一串字符以及一些有项目符号的项目。Builder类规定组成文件的方法,Director类利用这个方法产生一份具体的文件。图6-1显示了各个类间的关系。
以下是Java语言实现,能够正确编译通过。
[Java代码]
//Builder. java文件
public (1) class Builder {
public abstract void makeTitle(String title);
public abstract void makeString(String str);
public abstract void makeItems(String[] items);
public abstract Object getResult();
}
//Director. java文件
public class Director{
private (2) builder;
public Director(Builder builder){
this. builder = builder;
}
public Object construct(){
builder.makeTitle("Greeting");
builder.makeString("从早上到白天结束");
builder.makeItems(new String[]{"早安", "午安",});
builder.makeString("到了晚上");
builder.makeItems(new String[]("晚安", "好梦",});
return builder.getResult();
}
}
//TextBuilder.java文件
public class TextBuilder (3) Builder{
private StringBuffer buffer = new StringBuffer();
public void makeTitle(String title){
buffer.append("『" + title + "』"\n\n");
}
public void makeString(String str){
buffer.append('■' + str + "\n\n ");
}
public void makeItems(String[] items){
for(int i = 0; i< (4) ; i++){
buffer.append('·' + items[i] + "\n");
}
buffer.append("\n");
}
public Object getResult(){
return buffer.toString();
}
}
//Main.java文件
public class Main {
public static void main(String[] args) {
Director director = new Director(new TextBuilder());
String result = (String)director. (5) ;
System.out.println(result);
第8题:
请阅读下面程序 public class ExampleStringBuffer{ public static void main(String []args){ StringBuffer sb=new StringBuffer("test"); System.out.println("buffer="+sB) ; System.out.println("length="+sb.length());} } 程序运行结果中在"length="后输出的值
A.10
B.4
C.20
D.30
第9题:
Class TestException 1. public class TestException extends Exception { 2. } Class a: 1. public class a { 2. 3. public String sayHello(String name) throws TestException { 4. 5. if(name == null) { 6. throw new TestException(); 7. } 8. 9. return “Hello “+ name; 10. } 11. 12. } A programmer wants to use this code in an application: 45. A a=new A(); 46. System.out.println(a.sayHello(”John”)); Which two are true?()
第10题:
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?()