2

I was looking some methods of PrintStream class and came across write() and as per the docs to print in the console we need to call System.out.flush();.But my doubt is if I write these lines

System.out.write(40);
System.out.write(10);

Then also ( gets printed.I know 10 represents new line but I wanted to know why it only happens with new line.If I write

 System.out.write(40);
 System.out.write(32); 32 for space then also nothing gets printed.

Demonstration

Leri
  • 11,559
  • 5
  • 38
  • 59
SpringLearner
  • 13,195
  • 20
  • 69
  • 111
  • The answers below have covered it but this answer gives some additional related information: http://stackoverflow.com/a/7166357/3025921 – thesquaregroot Dec 27 '13 at 07:22
  • You have linked the documentation, which itself contains the answer: *`If the byte is a newline and automatic flushing is enabled then the flush method will be invoked.`* I am not sure why you are asking this question. – Sage Dec 27 '13 at 07:25
  • @Sage Because my own expected behavior would be that while `System.out` might not get dumped to stdout for a while, surely any remaining output would get printed when the program exits. – chrylis -cautiouslyoptimistic- Dec 27 '13 at 07:27
  • @chrylis, yes i understand. But as soon as we look into the implementation details our expectations become justified. In fact i also wanted to answer the question, but i re-read and clicked on the documentation OP himself linked: i just stunned to see that the answer is already there and got confused what really he is after! If he asked that he wants something like clarification then i would not bother but answer it. Please, don't get me wrong :) – Sage Dec 27 '13 at 07:38

4 Answers4

5

From the PrintStream.write:

if ((b == '\n') && autoFlush)
   out.flush();
}

So if you write a new-line to a System.out it will be auto-flushed. Btw javadoc says that too:

Writes the specified byte to this stream. If the byte is a newline and automatic flushing is enabled then the flush method will be invoked.

denis.solonenko
  • 11,179
  • 2
  • 26
  • 22
1

Probably the output stream is line-buffered - that is, it automatically flushes when it sees an end-of-line. To guarantee your ouput you should always use flush().

davmac
  • 18,558
  • 1
  • 32
  • 55
1

This appears to be a race condition in PrintStream. The (OpenJDK 6) PrintStream code forces a flush of the underlying OutputStream whenever the output written includes the character \n, but not any other time, and there's no sort of finalizer that ensures that buffered output in the BufferedOutputStream the JVM wraps around standard output gets flushed when the JVM exits.

The code that initializes the System class uses a 128-byte buffer for System.out, and if you output enough characters to fill the buffer (I inserted a 128-count for loop around writing the ( character), you'll see the output on the terminal.

chrylis -cautiouslyoptimistic-
  • 67,584
  • 19
  • 106
  • 140
1

From the docs.

public void write(int b)

Writes the specified byte to this stream. If the byte is a newline and automatic 
flushing is enabled then the flush method will be invoked.
4J41
  • 4,641
  • 25
  • 39