当前位置:IT认证>软件水平

问题:[选择题]

[单项选择题] 请阅读下面程序 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(Stringstr)构造方法,用str给出字符串的初始值,并分配16个字符的缓存。因此,字符串sb的初始值是“test”,并且包含16个字符的缓存。leng出()方法用来获得字符申长度,不包含缓存。故程序运行结果中在“length=”后输出的值应该是字符串sb的长度,即4。

微信端