Java BASICS (System. err and System. out), policystem. err

Source: Internet
Author: User

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 (VariableoutIs 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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.