The difference between System.out.println and System.err.println

Source: Internet
Author: User

Using both System.out.println and System.err.println () to print the input, the result is different from what is expected, and the order is not the same as expected because of the difference between err and out, but because they are two streams, The output order is different because of the caching reason.


1, System.out.println can redirect to other output streams, so that you will not be able to see the printed things on the screen, and System.err.println can only be printed on the screen, even if you redirect the same.
System.setout (New PrintStream (New FileOutputStream ("C:/test.txt")));
System.out.println ("haha");


2, When you output information to the console, the developer has two choices: System.out and System.err. The user is more inclined to output is system.out, and if it is System.err output "error". Although this may seem obvious, many developers do not understand why errors and debugging use System.err. (If you use Err to print out a string, the console in eclipse will appear in red.) When outputting a stream, the JVM and the operating system collectively decide when to output the stream. That is, although the developer has typed:

System.out.print_
("Test Output:");
The combination of the JVM and the operating system does not immediately output this stream. Instead, it will keep waiting until the output reaches a certain amount.
Suppose you enter the following directive:
System.out.println ("Debugging Info.");
The JVM may agree to the output, however, the operating system may decide not to output.
For this reason, it is possible to become a problem when you debug a program by trying to find out where it went wrong. Consider the following procedures:

for (int i=0;   i<56; i++) {
System.out.println (i);
..//containing an error
}
The error may occur at I equals 54 o'clock, but the JVM may end the output at I equals 49 o'clock. 50 to 54 still exist in the cache, and the result is lost.
You can avoid this by using System.err to report errors and debugging programs, which will result in the output of each operation. For example, the following programs:

for (int i=0;   i<56; i++) {
System.err.println (i);
..//containing an error
}

An error message is displayed every time I equals 54 o'clock.


3, System.out.println may be buffered, and SYSTEM.ERR.PRINTLN will not


4, System.err and System.out are error output and standard output
If you use log4j to log, and you set the wrong level,

The output of the System.err is recorded in the log


5, the output device is the same, so you see is the same

System.seterr () System.setout () is a method of redirecting two streams.
The following is a little more general in the Chinese documentation for Sun JDK1.5
------------------------------
System.err
"Standard" error output stream. This stream is open and ready to accept output data.

Typically, this stream corresponds to the display output or another output target specified by the host environment or user. By convention, this output stream is used to display an error message, or to show that even if the user's output stream (the value of the variable out) has been redirected to a file or other target that is not normally monitored continuously, it should also immediately cause additional information that the user is aware of.

System.out "standard" output stream. This stream is open and ready to accept output data. Typically, this stream corresponds to the display output or another output target specified by the host environment or user.


6, System.err.println () is to buffer, so the priority will be high, and System.out.println () does not need to buffer, so the priority will be low.

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.