Home >Java >javaTutorial >Some issues using System.err and System.out.
Today a colleague encountered some minor problems using System.err and System.out.
After reading some information, I summarized:
1. The JDK documentation explains the two:
Out: "Standard" output stream. This stream is open and ready to accept output data. Typically, this stream corresponds to monitor output or another output destination specified by the host environment or the user.
err: "Standard" error output stream. This stream is open and ready to accept output data. Typically, this stream corresponds to monitor output or another output destination specified by the host environment or the user. By convention, this output stream is used to display error messages, or to display information that should be displayed immediately even if the user output stream (the value of variable out
) has been redirected to some file or other destination that is not normally continuously monitored. Other information that is brought to the user's attention.
2One difference between.out and err is that out is often cached, while err is not cached (the default setting can be changed). So if you use standard error to print something, it can be displayed on the screen immediately, while what standard output prints may take a few more characters to be printed together. You may see this problem if you mix standard output and standard error in your application.
Test code:
public class Test2 {static{ System.out.println("1"); }public static void main(String[] args) { System.err.println("2");new Test2(); }public Test2() { System.out.println("3"); } }
Test results: The positions of 1 and 3 are relatively unchanged, and the position of 2 appears randomly. Try to avoid mixing them!
3.If log4j logging is used, System.err will be recorded in the log, and System.out will not
ps: Printing System.err is red in eclipse. System.out is blue.
The above is the detailed content of Some issues using System.err and System.out.. For more information, please follow other related articles on the PHP Chinese website!