62,620
社区成员
发帖
与我相关
我的任务
分享
public void println(Object x) {
synchronized (this) {
print(x);
newLine();
}
}
public void print(Object obj) {
write(String.valueOf(obj));
}
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
* <p>
* The Java language provides special support for the string
* concatenation operator ( + ), and for conversion of
* other objects to strings. String concatenation is implemented
* through the <code>StringBuilder</code>(or <code>StringBuffer</code>)
* class and its <code>append</code> method.
然后你的问题就很容易理解了,
package A;
public class NullTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO XWu Auto-generated method stub
StringBuilder sb = new StringBuilder("");
sb.append((Object)null); //1
//sb.append(((Object)null).toString()); //2
System.out.println(sb);
}
}
第一个append当然是可以执行的
第二个append是nullpoint异常。
和你直接用+的结果一致