算法题
用append()追加内容null,字符串长度为4,字符串输出为"null"。
查看源码可知,append()是将null作为字符’n’,‘u’,‘l’,‘l’,添加至char形数组并返回。
用构造器方式将null赋值,此时程序会报空指针异常NullPointerException。
String str = null;
StringBuffer sb = new StringBuffer();
sb.append(str);
System.out.println(sb.length()); //4
System.out.println(sb); //"null"
StringBuffer sb1 = new StringBuffer(str); //空指针异常NullPointerException
System.out.println(sb1);