A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© suifeng1987 中级黑马   /  2016-8-23 08:01  /  665 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

String  s = null ;System .out .println(s);出来的结果是null 我记得老师说过打印时默认调用toString方法,那相当于打印的是null 调用的toString那为什么没有空指针异常??

4 个回复

倒序浏览
public void print(String s) {
        if (s == null) {
            s = "null";
        }
        write(s);
    }

查看println()和print()的源码,你会发现这个方法对字符串进行了判断,如果值为null,就将"null"赋值给字符串。

点评

哈哈哈 ,正把我给愁到了,差点忘了  发表于 2016-8-23 23:14
回复 使用道具 举报
阿卜 发表于 2016-8-23 17:51
public void print(String s) {
        if (s == null) {
            s = "null";

谢了原来是print方法的问题
回复 使用道具 举报
阿卜 发表于 2016-8-23 17:51
public void print(String s) {
        if (s == null) {
            s = "null";

真是受教了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马