Why does System.out.print cause autorun?

System.out is a PrintStream object. I read the documentation for PrintStream. I do not understand why System.out.print causes a buffer reset? Shouldn't this just happen for println?

+4
source share
3 answers

Shouldn't this be just for println?

Javadoc does not say when it will not turn red. And he says that he will be cleared of println() or a newline.

+1
source

At the risk of repeating the facts that have already been noted, let me try to interpret the document a little differently ...

It seems that only with PrintBuffer the creation time (i.e. when calling the constructor) can AutoFlush behavior for PrintStream be configured.

In addition, as you indicated, the documentation states that when you call any of the various public PrintBuffer constructors without specifying the startup status, a non-automatic PrintStream will be created.

However, in the case of System.out, you do not invoke the constructor for PrintBuffer. The java.lang.System class creates an "out" PrintStream instance when the VM starts. This means that when you request a PrintStream object, which is stored in the System object in its drop-down field, you don’t know which constructor was called, and therefore there is no idea of ​​the autostart state of the stream that will be passed to you when you ask for him.

I agree, it would be very nice if the doc for java.lang.System indicated that the stream contained in its "out" field has its autostart mode equal to true. But this is not a “requirement”, no more than I should document if JButton returned from my (hypothetical) myCrazyPanel.getTheChangeColorsButton () on or off. Yes, the default buttons but you cannot complain if JButton is disabled. The same thing here.

+1
source

You want to flush the buffer when you call System.out.print () because you want it printed. When I call for printing, I want her to print something. If he has not cut himself, he will simply remain in the buffer, and I will not see anything.

Check out the flash here .

In principle, this is a guarantee that it will be printed immediately.

0
source

Source: https://habr.com/ru/post/1440176/


All Articles