📌  相关文章
📜  Java中 System.out.println() 和 System.err.println() 的区别

📅  最后修改于: 2022-05-13 01:54:50.106000             🧑  作者: Mango

Java中 System.out.println() 和 System.err.println() 的区别

System.out是我们可以写入字符的 PrintStream。它在命令行界面控制台/终端上输出我们写入的数据。它主要用于控制台应用程序/程序向用户显示结果。它也可用于调试小程序。

句法:

System.out.println("Your Text which you want to display");

例子:

Java
// Java program to Demonstrate Use of System.out.println()
  
// Importing required input output classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Print statement
        System.out.println("GeeksForGeeks!");
    }
}


Java
// Java Program to Demonstrate Use of System.err.println()
  
// Importing required classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Print statement
        System.err.println("GeeksForGeeks!");
    }
}


输出
GeeksForGeeks!

现在让我们进入 System.err 的下一个概念,它也与 Syste.out 密切相关。 System.err也是一个打印流。它的工作原理与 System.out 相同。它主要用于输出错误文本。某些程序(如 Eclipse)将以红色文本显示 System.err 的输出,以使其更明显是错误文本。

句法:

System.err.println("Your Text which you want to display");

例子

Java

// Java Program to Demonstrate Use of System.err.println()
  
// Importing required classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Print statement
        System.err.println("GeeksForGeeks!");
    }
}

输出:

GeeksForGeeks!

笔记:

  • System.err 和 System.out 都在 System 类中定义为 PrintStream 类的引用变量:
public final static PrintStream out = null;
public final static PrintStream err = null;
  • 两个输出都显示在同一个控制台上,大多数 IDE 用红色区分错误输出。
  • 我们可以重新配置流,例如,System.out 仍会打印到控制台,但 System.err 会写入文件。

现在让我们最后总结一下两者之间的差异,下面以表格形式描述如下:

System.out.println()System.err.println() 
System.out.println() will print to the standard out of the system.System.err.println() will print to the standard error.
System.out.println() is mostly used to display results on the console.System.err.println( is mostly used to output error texts.
It gives output on the console with the default(black) color.It also gives output on the console but most of the IDEs give it a red color to differentiate.