javajvmsystem.out

How does the JVM print something to the screen?


So I've been studying Java-bytecode and it's been fun so far, seeing how the JVM works. But there's something confusing me.

For example, if the JVM wants to do any sort of math, it uses registers to push/pop off the internal stack (oversimplifying it, I know).

But let's say the JVM wants to compute the bytecode version of this line: System.out.println("Hello");

In the mnemonic version of the bytecode, I can see it searches for the System class in the java.lang package and invokes that method but what does that actually mean for the JVM?

Once the JVM reads the instruction and knows it has to print something, does it spawn a console window? Or can the JVM only do this type of instruction FROM the console? (So like for this, you would NEED to use the java tool)

Thank you!


Solution

  • Any text written to System.out is sent to the standard output stream of the Operating System Process running the JVM.

    The Operating System then decides what that means, which usually means printing the text to the console, if there is one.

    It may also mean to write the text to a file, if standard output is redirected to a file, e.g. using the > syntax in a shell. Or it could mean sending the text to another process if standard output is piped, e.g. using the | syntax in a shell.

    The JVM never "knows" it has to print something. The method is named print as a convenience to programmers for historical reasons, but is really no different from write.