简体   繁体   English

为什么System.out.print导致自动刷新?

[英]Why does System.out.print cause autoflush?

System.out is a PrintStream object. System.out是一个PrintStream对象。 I read the documentation on PrintStream. 我阅读了有关PrintStream的文档。 What I don't get is why System.out.print causes the buffer to be flushed? 我不明白的是为什么System.out.print导致缓冲区被刷新? Shouldn't that happen only for println? 那不是仅对println会发生吗?

Shouldn't that happen only for println? 那不是仅对println会发生吗?

The Javadoc doesn't say when it won't be flushed. Javadoc不会说何时刷新。 And it says it will be flushed on a println() or a newline . 它说,这是一个刷新println() 或一个换行符

At the risk of repeating the facts that have already been noted, let me try to interpret the doc a little differently... 冒着重复已经提到的事实的风险,让我尝试对文档进行一些不同的解释...

It seems that it's only at PrintBuffer creation time (that is, during constructor invocation) that the autoFlush behavior of a PrintStream can be set. 似乎只有在PrintBuffer 创建时 (即,在构造函数调用期间)才可以设置PrintStream的autoFlush行为。

Also, as you've pointed out, the documentation states that, when calling any of the various public PrintBuffer constructors, not specifying the autoflush state will result in a non-autoflushing PrintStream being created. 另外,正如您所指出的,文档指出,在调用各种公共PrintBuffer构造函数中的任何一个时,不指定自动刷新状态将导致创建非自动刷新PrintStream。

However, in the System.out case, you are not calling the construtor for the PrintBuffer. 但是,在System.out情况下, 不是在调用PrintBuffer的构造函数。 The java.lang.System class instantiates the "out" PrintStream on VM startup. java.lang.System类在VM启动时实例化“输出” PrintStream。 That means that, when you request the PrintStream object that the System object stores in its "out" field, you have no idea which constructor was called, and therefore no idea of the autoflush state of the stream that gets handed to you when you ask for it. 这意味着,当您请求将System对象存储在其“ out”字段中的PrintStream对象时,您不知道调用了哪个构造函数,因此也不知道当您询问时流交给您的流的自动刷新状态为了它。

I agree, it would have been really nice if the doc for java.lang.System specified that the stream contained in its "out" field has its autoflush behavior set to true. 我同意,如果java.lang.System的文档指定其“ out”字段中包含的流的autoflush行为设置为true,那将是非常好的。 But that's not a "requirement," any more than I'm required to document whether the JButton returned from my (hypothetical) myCrazyPanel.getTheChangeColorsButton() is enabled or disabled. 但这不是“要求”,仅是我需要记录从(假设的)myCrazyPanel.getTheChangeColorsButton()返回的JButton是否需要记录。 Yeah, buttons are enabled by default , but you're not allowed to complain if the JButton is disabled. 是的, 默认情况下按钮是启用 ,但是如果JButton被禁用,您就不会抱怨。 Same thing here. 这里也是一样。

You want the buffer to be flushed when you call System.out.print() because you want it to be printed. 您希望在调用System.out.print()时刷新缓冲区,因为您希望将其打印出来。 When I call print, I want it to print something. 当我打电话给print时,我希望它打印一些东西。 If it didn't flush, it would just remain in the buffer and I wouldn't see anything. 如果不刷新,它只会保留在缓冲区中,而我什么也看不到。

Check out flush here . 在这里检查冲洗。

Basically it's a guarantee that it'll get printed immediately. 基本上,这是可以立即打印的保证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM