So from my understanding, the PrintStream must use some sort of Java Native Interfacing to communicate with the Operating System so it can write to the standard output, or does java use some other technique? I would like to know as the JVM's architectures intrigues me. It is very interesting to me understand the way it works and the architecture of the system itself.
-
grepcode.com usually is a good sport to find out such things; in this case however, I didn't get a satisfying conclusion: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/sun/tools/jconsole/OutputViewer.java#46 seems to point to System.setOut(PipeListener.create("System.out")); which appear to be Unix pipe listeners... from there on it gets hazy... but in the end, YES, it will be JVM-specific implementations that access the OS via JNI. Sorry I couldn't help more. – JayC667 Nov 22 '15 at 23:52
-
That's fine. I hit the same deadend when looking for the answer, which is why I came here thanks! – TheRenegade Nov 22 '15 at 23:56
2 Answers
Standard output stream in OpenJDK is a PrintStream
which wraps BufferedOutputStream
, which wraps FileOutputStream
which is created from FileDescriptor
. There are special FileDescriptor
objects which correspond to the stdin, stdout and stderr (in particular, see FileDescriptor.out
). They have well known numbers (for example, stdout file descriptor is 1
). So the real logic is inside the FileOutputStream.writeBytes
method which is of course native. On the Java side we have buffering, synchronization and translation of characters into bytes. The low-level stuff (writing bytes directly to the file descriptor) is done by native code.

- 97,161
- 19
- 222
- 334
The Java System.out
PrintStream
writes to stdout. Wikipedia describes stdout as,
The file descriptor for standard output is 1 (one); the POSIX
<unistd.h>
definition isSTDOUT_FILENO
; the corresponding<stdio.h>
variable isFILE* stdout
; similarly, the<iostream>
variable isstd::cout
.
While the Javadoc for System.out
says (in part)
The "standard" output stream. This stream is already open and ready to accept output data.

- 198,278
- 20
- 158
- 249
-
Not necessarily, it's built into the JVM... so it might be native code (it might just write to an underlying C++ stream) or it might write to a file handle. – Elliott Frisch Nov 22 '15 at 23:52