package java1; import org.junit.Test; public class IDEADebug { @Test public void testStringBuffer(){ String str = null;//String类型存地址值,此处存null,空指针 StringBuffer sb = new StringBuffer(); sb.append(str); //此处底层,变成添加四个字符 //private AbstractStringBuilder appendNull() { // int c = count; // ensureCapacityInternal(c + 4); // final char[] value = this.value; // value[c++] = 'n'; // value[c++] = 'u'; // value[c++] = 'l'; // value[c++] = 'l'; // count = c; // return this; // } System.out.println(sb.length());//4 System.out.println(sb);//"null" StringBuffer sb1 = new StringBuffer(str);//抛异常NullPointerException //此处底层是str.length()等于null.length()调用了空指针了 // public StringBuffer(String str) { // super(str.length() + 16); // append(str); // } System.out.println(sb1); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)