Java BASICS (System. err and System. out), policystem. err
Today, a colleague encountered some minor problems when using System. err and System. out.
Summary:
1. JDK document:
Out:"Standard" output stream. This stream is enabled and ready to accept output data. Generally, this stream corresponds to the display output or another output target specified by the host environment or the user.
Err:"Standard" error output stream. This stream is enabled and ready to accept output data. Generally, this stream corresponds to the display output or another output target specified by the host environment or the user. By convention, this output stream is used to display error messages, or display those even if the user outputs a stream (Variableout
Is redirected to a file or other target that is not continuously monitored, and other information that should be immediately noticed by the user.
2One difference between. out and err is that out usually carries cache, while err does not (default setting can be changed ). Therefore, if you print out items with a standard error, they can be immediately displayed on the screen, and the printed items may have to accumulate a few characters before they can be printed together. If you mix standard output and standard errors in the application, you may see this problem.
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: positions 1 and 3 remain unchanged, and positions 2 appear randomly. Try to avoid mixing them!
3.If log4j logging is used, System. err will be logged, and System. out will not
Ps: print that System. err is red in eclipse. System. out is blue.