What is System.out exactly?

徘徊边缘 提交于 2021-02-04 17:53:05

问题


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:

  • System.out.println in jar

    There is no problem doing like that. But where do you expect to see the output?

  • What happens to “System.out.println()” in executable jar?

    If you run the code in some way that doesn't attach a console - such as javaw on Windows, which is the default program associated with executable jar files - then the output won't go anywhere. It won't cause any errors - the text will just be lost.

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?

回答1:


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




回答2:


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.




回答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();
  }
}


来源:https://stackoverflow.com/questions/32287816/what-is-system-out-exactly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!