difference between System.out.println() and System.err.println()

后端 未结 7 623
情书的邮戳
情书的邮戳 2020-12-12 16:16

What is the difference between System.out.println() and System.err.println() in Java?

7条回答
  •  难免孤独
    2020-12-12 16:40

    It's worth noting that an OS has one queue for both System.err and System.out. Consider the following code:

    public class PrintQueue {
        public static void main(String[] args) {
            for(int i = 0; i < 100; i++) {
                System.out.println("out");
                System.err.println("err");
            }
        }
    }
    

    If you compile and run the program, you will see that the order of outputs in console is mixed up.

    An OS will remain right order if you work either with System.out or System.err only. But it can randomly choose what to print next to console, if you use both of these.

    Even in this code snippet you can see that the order is mixed up sometimes:

    public class PrintQueue {
        public static void main(String[] args) {
            System.out.println("out");
            System.err.println("err");
        }
    }
    

提交回复
热议问题