What's the Meaning of System.Out.Println in Java

What's the meaning of System.out.println in Java?

No. Actually out is a static member in the System class (not as in .NET), being an instance of PrintStream. And println is a normal (overloaded) method of the PrintStream class.

See http://download.oracle.com/javase/6/docs/api/java/lang/System.html#out.

Actually, if out/err/in were classes, they would be named with capital character (Out/Err/In) due to the naming convention (ignoring grammar).

What is System, out, println in System.out.println() in Java

The first answer you posted (System is a built-in class...) is pretty spot on.

You can add that the System class contains large portions which are native and that is set up by the JVM during startup, like connecting the System.out printstream to the native output stream associated with the "standard out" (console).

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

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.

What is the use of System.out::println in Java 8

It's called a "method reference" and it's a syntactic sugar for expressions like this:

numbers.forEach(x -> System.out.println(x));

Here, you don't actually need the name x in order to invoke println for each of the elements. That's where the method reference is helpful - the :: operator denotes you will be invoking the println method with a parameter, which name you don't specify explicitly:

numbers.forEach(System.out::println);

What is System.out exactly?

Processes in modern operating systems (and for that matter, several older operating systems) get three standard "streams" associated with them:

  • Standard in: Stream-based input (stdin)
  • Standard out: Stream-based output (stdout)
  • Standard error: Stream-based error output (stderr)

Collectively (and creatively) they're called the standard streams.

System.in, System.out, and System.err are, by default, Java's standard mechanism for writing to those streams.

Programs invoked from the command line are run in an environment where keystrokes in the command line go to stdin, and the output of both stdout and stderr shows as text in the console. They can be redirected to files, etc.

Programs launched via GUIs frequently don't have those streams hooked to anything you can see.

I say "by default" above because you can use calls on System to change where those streams point (they have creative names like setIn, setOut, and setErr.)

How do I open the console from a Runnable JAR file started by user with double-click?

There's a false correlation there: The fact that the jar is runnable is not why you don't see the streams. If you run a runnable jar at the command line, you'll see its standard output.

Whether you can see that output if you run it without it being associated with a console of some kind will depend on how you're running it and, potentially, how it's written. Many GUI frameworks will redirect standard out and err to a log file. Or the app may offer debugging options that do so. There's no one standard answer there (no pun).



Related Topics



Leave a reply



Submit