System.out.println与System.err.println的区别

输出顺序最重要!!!

总结:

  • err是运行期异常和错误反馈的输出流的方向
  • System.err.println只能在屏幕上实现打印,即使你重定向了也一样
  • 用err打印出的 字符串,再eclipse的console会显示成红色
  • 标准输出往往是带缓存的,而标准出错没有缓存(默认设置,可以改)

可以参考如下代码:

public class TestCodeSeg{  
    static{  
        System.out.println("1");  
    }  
    {  
        System.out.println("2");  
    }  
    public TestCodeSeg(){  
        System.err.println("3");  
    }  
    public static void main(String[] args){  
        new TestCodeSeg();  
    }  
}  

结果:1、2顺序不变,3输出不定,位置不定

原因:System.out.println输出有缓存,System.err.println是立即输出,可能在输出1或2,还没有输出换行时输出3。

猜你喜欢

转载自blog.csdn.net/weixin_42124622/article/details/81353662