Home > Java > javaTutorial > body text

Some issues using System.err and System.out.

PHP中文网
Release: 2017-06-20 10:09:27
Original
1998 people have browsed it

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");
    }
    
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template