Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does System.out.println() in Java print to the console?

Tags:

java

I read several posts online explaining what System.out.println() is in Java. Most of them go like this:

  • System is a final class in the java.lang package.
  • out is a public static object inside the System class of type PrintStream.
  • println() prints a line of text to the output stream.

My question is when we do System.out.println() in our code, why does it end up writing to the console? This article explains how we can make it write to a file by calling System.setOut(). So my question translates to where is System.setOut() called to redirect its output to the console?

I checked System.setOut()'s source. It makes a call to setOut0() which is a native method. This method is directly called inside the initializeSystemClass() method by passing it fdOut which is a FileOutputStream defined here. I did not find a console output stream passed to setOut0() anywhere, nor did I find a call to the non-native setOut() done anywhere. Is it done somewhere else outside the System class by the JVM while starting execution? If so, can someone point me to it?

like image 215
anir Avatar asked Apr 12 '20 08:04

anir


People also ask

Where does system out Println print to?

The println is a method of java. io. PrintStream. This method is overloaded to print message to output destination, which is typically a console or file.

What is the difference between print () and println () when printing out to the console?

The prints method simply print text on the console and does not add any new line. While println adds new line after print text on console.

How do you print to console in Java?

println(): println() method in Java is also used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the start of the next line at the console. The next printing takes place from next line.

Why do we write system out Println for printing in Java explain in detail?

It is used when you want the result in two separate lines. It is called with "out" object. If we want the result in two separate lines, then we should use the println() method. It is also an overloaded method of PrintStream class.

What is the difference between system out () and println () in Java?

It prints the argument passed, into the System.out which is generally stdout. Here System is a class, out is a variable, and println () is a method. System is a class in the java.lang package . The out is a static member of the System class, and is an instance of PrintStream . The println is a method of PrintStream.

How to print text in the console in Java?

The “System.out.print” method is a preprogrammed method in Java for printing text in the console. How to print text and results in Java? When programming, you often want to print a text. For example, it may be the answer to a calculation or an error message. Java has lots of built-in features to make it easier when coding, and print is one of them.

What is the use of println() method of PrintStream in Java?

From the use of println () we observed that it is a single method of PrintStream class that allows the users to print various types of elements by accepting different type and number of parameters. System.out.println (), System.out.println (int), System.out.println (double), System.out.println (string), System.out.println (character), etc.

What is system out in Java?

System is a final class in the java.lang package. out is a public static object inside the System class of type PrintStream. println () prints a line of text to the output stream. My question is when we do System.out.println () in our code, why does it end up writing to the console?


2 Answers

My doubt is when we do System.out.println() in our code, why it ends up in writing to console?

In any POSIX compliant shell, each process gets three "standard" streams when the shell starts it:

  • The "standard input" stream is for reading input.
  • The "standard output" stream is for writing ordinary output.
  • The "standard error" stream is for writing error output.

(The same idea is also used in many non-POSIX compliant shells as well.)

For an interactive POSIX shell, the default is for these streams to read from and write to the shell's "console" ... which could be a physical console, but is more likely to be a "terminal emulator" on the user's (ultimate) desktop machine. (Details vary.)

A POSIX shell allows you to redirect the standard streams in various ways; e.g.

$ some-command < file     # read stdin from 'file'
$ some-command > file     # write stdout to 'file'
$ some-command 2> file    # write stderr to 'file'
$ some-command << EOF     # read stdin from a 'here' document
  lines of input
  ...
  EOF
$ some-command | another  # connect stdout for one command to
                          # stdin for the next one in a pipeline

and so on. If you do this, one or more of the standard streams is NOT connected to the console.

Further reading:

  • "What are stdin, stdout and stderr on Linux?"
  • "Standard Streams"

So how does this relate to the question?

When a Java program start, the System.in/out/err streams are connected to the standard input / output / error streams specified by the parent process; typically a shell.

In the case of System.out, that could be the console (however you define that) or it could be a file, or another program or ... /dev/null. But where the output goes is determined by how the JVM was launched.

So, the literal answer is "because that is what the parent process has told the Java program to do".


How internally shell communicates with jvm to set standard input / output in both Windows and Linux?

This is what happens with Linux, UNIX, Mac OSX and similar. (I don't know for Windows ... but I imagine it is similar.)

Suppose that the shell is going to run aaa > bbb.txt.

  1. The parent shell forks a child process ... sharing the parent shell's address space.
  2. The child process closes file descriptor 1 (the standard output file descriptor)
  3. The child process opens "bbb.txt" for writing on file descriptor 1.
  4. The child process execs the "aaa" command, and it becomes the "aaa" command process. The file descriptors 0, 1, and 2 are preserved by the exec call.
  5. The "aaa" command starts ...

When the "aaa" command starts, it finds that file descriptors 0 and 2 (stdin and stderr) refer to the same "file" as the parent shell. File descriptor 1 (stdout) refers to "bbb.txt".

The same thing happens when "aaa" is the java command.

like image 105
Stephen C Avatar answered Sep 22 '22 07:09

Stephen C


It doesn't need to. We can redirect to somewhere else. Here is the code to re-direct into the file:

PrintStream output = new PrintStream(new File("output.txt"));
System.setOut(output);
System.out.println("This will be written to file");

By default, the console is the standard output stream (System.in) in Java.

like image 23
hqt Avatar answered Sep 22 '22 07:09

hqt