Given this method in a class: public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append(‟ buffer.append(this.name); buffer.append(‟>‟); return buffer.toString(); } Which is true?()
A: This code is NOT thread-safe.
B: The programmer can replace StringBuffer with StringBuilder with no other changes.
C: This code will perform well and converting the code to use StringBuilder will not enhance the performance.
D: This code will perform poorly. For better performance, the code should be rewritten: return “”;
A: This code is NOT thread-safe.
B: The programmer can replace StringBuffer with StringBuilder with no other changes.
C: This code will perform well and converting the code to use StringBuilder will not enhance the performance.
D: This code will perform poorly. For better performance, the code should be rewritten: return “”;
举一反三
- 有如下代码: StringBuffer buffer = new StringBuffer("Happy Birthday");buffer.insert(2, '5');System.out.println(buffer);则最后输出的结果是( ) A: Hap5py Birthday B: H5appy Birthday C: Ha5py Birthday D: Ha5ppy Birthday
- 在J2EE中,test.jsp文件如下: <;html>; <;%{%>; <;jsp:useBean id="buffer" scope="page" type="java.lang.StringBuffer"/>; <;% buffer.append("ABC");%>; <;%}%>; buffer is <;%=buffer%>; <;/html>; 运行时,将发生( )。 A: 编译期错误 B: 运行期错误 C: 运行后,浏览器上显示:buffer is null D: 运行后,浏览器上显示:buffer is ABC
- 通常,被称为可变字符串的类是( )。 A: String B: Buffer C: StringBuffer D: Char
- 通过StringBuffer数组构造字符串对象的方法是( ) A: String(byte[ ] bytes) B: String(char[ ] value) C: String(Sting original) D: String(StringBuffer buffer)
- 读代码:public class foo {static String s;public static void main (String[]args) {System.out.println (“s=” + s);}}结果是: A: The code compiles, but a NullPointerException is thrown when toString is called B: The code compiles and “s=null” is printed C: The code does not compile because string s cannot be referenced D: The code does not compile because string s is not initialized E: The code compiles and “s=” is printed