问题
单项选择题
请阅读下面程序 publicclassExampleStringBuffer{ publicstaticvoidmain(String[]args){ StringBuffersb=newStringBuffer("test"); System.out.println("buffer-,"+sb); System.out.println("longth="+sb.1ength());}} 程序运行结果中在“length”后输出的值是( )。
A.10
B.4
C.20
D.30
答案
参考答案:B
解析: 本题对StfingBuffer(String str)构造方法,用str给出字符串的初始值,并分配16个字符的缓存。因此,字符串sb的初始值是“test”,并且包含16个字符的缓存。leng出()方法用来获得字符申长度,不包含缓存。故程序运行结果中在“length=”后输出的值应该是字符串sb的长度,即4。