What is System.out exactly?
Asked Answered
A

3

8

I noticed that any call to System.out.println() from a JAR file that hasn't been started by the command line (i.e. a Runnable JAR file started by user with double-click) won't open the console.

After doing some research, I found multiple answers on the site:

From what I understand, System.out does not represent the console. It does represent data which can be handled by anything that needs to display it. Am I right?

  • What is System.out exactly?
  • How do I open the console from a Runnable JAR file started by user with double-click?
Aquatic answered 29/8, 2015 at 15:45 Comment(0)
R
10

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).

Ressieressler answered 29/8, 2015 at 15:51 Comment(4)
Do programs invoked from the command line have redirected streams because the console itself is an application, that starts those using the normal Java process API, like any other one would do?Aquatic
@FrancescoMenzani: Programs invoked from the command line usually don't have redirected streams. Say you do java -r runnable.jar: The OS runs the java program, providing it with its standard streams. The java program loads the jar and does its work to set up the streams for System in the newly-loaded JVM to read from and output to the streams it got from the OS.Ressieressler
If they don't have redirected streams, then how is it possible for the console to handle their IO?Aquatic
@FrancescoMenzani: I wouldn't use "redirected" in that sense. The console's streams are provided to the child processes it launches (that's standard OS behavior any time a process launches a child process). The java tool then makes those available to the code it's running. To me that's not redirection, but it's probably a semantic point. (Thanks for the edit, btw; I always misspell corre...corro...um...that word.)Ressieressler
S
4

Here, System.out represents the output stream - where your output will go. By default it is set to console. But you can change it to other like - text file. Most often, in large application it is used for logging (usually by new programmer, bad idea). In this case you can see the output in appropriate log file.

Suberin answered 29/8, 2015 at 15:50 Comment(0)
H
3

System is final class from java.lang package(default package in java) and cannot be instantiated.

out is a static member field of System class and is of type PrintStream and its access specifiers are public final.

println – is an overloaded method of PrintStream class. println prints the argument passed to the standard console and a newline. There are multiple println overloaded methods with different arguments. Every println makes a call to print method and adds a newline. Internally, print calls write() and write() takes care of displaying data to the standard output window.

Here it is how it should look in the inside:

//the System class belongs to java.lang package
class System {
  public static final PrintStream out;
  //...
}

//the Prinstream class belongs to java.io package
class PrintStream{
public void println();
//...
}

We therefore don't need to ever instantiate a System object to print messages to the screen; we simply call the println method on the System class's public static PrintStream member, out.

But you cannot create an object of PrintStream and call the println function. When you want to print to the standard output, then you will use System.out. That's the only way. Instantiating a PrintStream will allow you to write to a File or OutputStream you specify, but don't have anything to do with the console.

However, you can pass System.out to PrintStream and then invoke println on PrintStream object to print to the standard output. Here is a small example:

import java.io.*;
public class SystemOutPrintlnDemo
{
  public static void main(String[] args) 
  {
    //creating PrintStream object
    PrintStream ps = new PrintStream(System.out);
    ps.println("Hello World!");
    ps.print("Hello World Again!");
    //Flushes the stream
    ps.flush();
  }
}
Hutson answered 29/8, 2015 at 15:50 Comment(3)
Although it may be correct, my question is not about syntax. I want to know the feature of the class and the world behind them.Aquatic
Check my updated answer, I hope it solves your doubt about the features. I suggest you go on this link for more answers on the System class.Hutson
Perhaps I should have said "purpose" instead of "feature". I don't want to know how Oracle developers implemented the System class, but how those streams work.Aquatic

© 2022 - 2024 — McMap. All rights reserved.