【Java基础】System.out.println() 解析

1.代码说明
image
System类提供一些有用的属性和方法,包括标准输入输出和错误打印。
有一个对象属性out,类型为PrintStream。
setOut()方法使用static修饰,类加载时执行。
该对象属性可以调用PrintStream类中的打印方法。

public final class System {
public final static PrintStream out = null;
public static void setOut(PrintStream out) {
checkIO();
setOut0(out);
}
}
public class PrintStream extends FilterOutputStream
implements Appendable, Closeable
{
public void println(String x) {
synchronized (this) {
print(x);
newLine(); //换行
}
}
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
private void write(String s) {
try {
synchronized (this) {
ensureOpen();
textOut.write(s);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush && (s.indexOf('\n') >= 0))
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
private void newLine() {
try {
synchronized (this) {
ensureOpen();
textOut.newLine();
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
}

PrintStream类中包含打印各种数据类型的重载方法。
image

posted @   植树chen  阅读(710)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示