System.out.println 流程详细解释
Yuxuan Wu Lv13

在java中,最经常使用的是System.out.println() 来进行打印输出

println方法会自动调用toString方法,如果不重写方法,直接对一个对象进行打印,一定会输出对象的类型+@+内存地址值

具体过程:

println首先调用的是对象的valueOf方法,而valueOf方法则是调用了对象的toString()方法,toString()方法原来是返回对象的格式化信息的,所以如果不重写toString(),那么就会打印这串字符的哈希值,重写的话就能把重写后的格式打印出来。

参考链接
println()源码:这个也解释了为什么println后面要换行,因为不只是print(x),还有一个newLine方法,用锁锁起来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* Prints an Object and then terminate the line. This method calls
* at first String.valueOf(x) to get the printed object's string value,
* then behaves as
* though it invokes {@link #print(String)} and then
* {@link #println()}.
*
* @param x The {@code Object} to be printed.
*/
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}

我们继续看print()的源代码

1
2
3
public void print(String s) {
write(String.valueOf(s));
}

String.valueOf()源码:

1
2
3
4
5
6
7
8
9
10
11
12
/**
* Returns the string representation of the {@code Object} argument.
*
* @param obj an {@code Object}.
* @return if the argument is {@code null}, then a string equal to
* {@code "null"}; otherwise, the value of
* {@code obj.toString()} is returned.
* @see java.lang.Object#toString()
*/
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}

这里obj.toString会调用toString()的方法

注意:这里return的时候还运用到了三元运算符的知识

x?y:z如果x==true,则结果为y,否则结果为z

return (obj == null) ? "null" : obj.toString();可以被理解为如果obj为空,那么结果就输出null字符串,否则钓鱼obj的toString 方法来打印

1
2
3
4
5
6
7
8
9
10
11
package operator;

public class Demo08 {
public static void main(String[] args) {
//x?y:z
//如果x==true,则结果为y,否则结果为z
int score = 80;
String type = score < 60 ? "不及格":"及格"; //必须掌握,让代码更加精简,且容易掌握
System.out.println(type);
}
}

toString()源码:

官方文档建议每一个子类都重写这个toString 方法:It is recommended that all subclasses override this method.

如果直接打印一个对象,发现并不是对象的类型+@+内存地址值,则其内部方法一定已经重写过toString()方法过了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Returns a string representation of the object. In general, the
* {@code toString} method returns a string that
* "textually represents" this object. The result should
* be a concise but informative representation that is easy for a
* person to read.
* It is recommended that all subclasses override this method.
* <p>
* The {@code toString} method for class {@code Object}
* returns a string consisting of the name of the class of which the
* object is an instance, the at-sign character `{@code @}', and
* the unsigned hexadecimal representation of the hash code of the
* object. In other words, this method returns a string equal to the
* value of:
* <blockquote>
* <pre>
* getClass().getName() + '@' + Integer.toHexString(hashCode())
* </pre></blockquote>
*
* @return a string representation of the object.
*/
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
  • Post title:System.out.println 流程详细解释
  • Post author:Yuxuan Wu
  • Create time:2021-04-23 23:19:23
  • Post link:yuxuanwu17.github.io2021/04/23/2021-04-24-System.out.println-流程详细解释/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.