问题分析
Java中的System.out.print()打印流会自动调用类的toString()方法,但是并不是在print()函数中就直接调用
追究
我们直接在idea中直接搜索System类时
![图片[1]-System.out.print()为什么会打印类的toString方法剖析 - 拾光赋-拾光赋](https://image.baidu.com/search/down?url=https://img-blog.csdnimg.cn/direct/0e2ef8554faf4ca587c5b5d804f79c46.png)
选中第一个
进入后找到打印流public static final PrintStream out = null;
选中 PrintStream 后 Ctrl+B 跟进
在PrintStream.java文件中搜索 print(
![图片[2]-System.out.print()为什么会打印类的toString方法剖析 - 拾光赋-拾光赋](https://image.baidu.com/search/down?url=https://img-blog.csdnimg.cn/direct/42553688ce1640ed82a7d164ebd73b89.png)
发现打印流写出的是String.valueOf后的Object对象
我们接着跟进valueOf()会发现默认的valueOf函数会判断对象是否为null,非null则调用类的toString方法![图片[3]-System.out.print()为什么会打印类的toString方法剖析 - 拾光赋-拾光赋](https://image.baidu.com/search/down?url=https://img-blog.csdnimg.cn/direct/6ac84ac3078042d3845c77ac3c417ef3.png)
而Object的toString()方法默认是输出类名+@+计算得出的哈希地址值
总结
System.out.print()会打印类的toString方法
© 版权声明
THE END


![表情[baoquan]-拾光赋](https://blogs.ink/wp-content/themes/zibll/img/smilies/baoquan.gif)


暂无评论内容