System.err.println输出位置不唯一确定

public static void main(String[] args) {
        int x=1;
        System.out.println("Before the loop");
        
        while (x<4) {
            System.err.println("in the loop");
            //System.out.println("in the loop");
            System.out.println("Vlaue of x is "+x);
            x=x+1;
        }
        
        System.out.println("after the loop");
                
    }

  根据代码执行顺序,应该得出这样的结果:

Before the loop
in the loop
Vlaue of x is 1
in the loop
Vlaue of x is 2
in the loop
Vlaue of x is 3
after the loop

但是,实际的运行结果:

 

 

System.out在JVM和操作系统都具有缓存功能,

就是你输出的东西不一定实时输出,有时候会积攒到一定数量才会输出

System.err实时输出(默认设置,可以改)

这也是为什么err打印位置不固定的原因

原文地址:https://www.cnblogs.com/panghao/p/14374325.html