Open In App

Difference Between System.out.println() and System.err.println() in Java

Last Updated : 03 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

System.out is a PrintStream to which we can write characters.  It outputs the data we write to it on the Command Line Interface console/terminal. It is mostly used for console applications/programs to display the result to the user. It can be also useful in debugging small programs.

Syntax:

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

Example:

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!");
    }
}


Output

GeeksForGeeks!

Now let us come onto the next concept of System.err which is also very closely related to System.out.System.err is also a print stream. It works the same as System.out. It is mostly used to output error texts. Some programs (like Eclipse) will show the output to System.err in red text, to make it more obvious that it is error text. 

Syntax:

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

Example

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!");
    }
}


Output:

GeeksForGeeks!

Note:

  • System.err and System.out both are defined in System class as reference variable of PrintStream class as:
public final static PrintStream out = null;
public final static PrintStream err = null;
  • Both outputs are displayed on the same console, Most of the IDEs differentiate the error output with red color.
  • We can reconfigure the streams so that for example, System.out still prints to the console but System.err writes to a file.

Now let us finally conclude out the differences between the two which are depicted below in tabular format as follows:

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.


Similar Reads

Difference Between System.out.print() and System.out.println() Function in Java
In Java, we have the following functions to print anything in the console. System.out.print() andSystem.out.println() But there is a slight difference between both of them, i.e. System.out.println() prints the content and switch to the next line after execution of the statement whereas System.out.print() only prints the content without switching to
2 min read
Redirecting System.out.println() Output to a File in Java
System.out.println() is used mostly to print messages to the console. However very few of us are actually aware of its working mechanism. We can use System.out.println() to print messages to other sources too, not just restricting it to the console. However, before doing so, we must reassign the standard output by using the following method of Syst
2 min read
System.out.println in Java
Java System.out.println() is used to print an argument that is passed to it. Parts of System.out.println()The statement can be broken into 3 parts which can be understood separately: System: It is a final class defined in the java.lang package.out: This is an instance of PrintStream type, which is a public and static member field of the System clas
5 min read
Difference between print() and println() in Java
print(): print() method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the end of the text at the console. The next printing takes place from just here. Various print() methods: void print(boolean b) - P
3 min read
PrintWriter println(String) method in Java with Examples
The println(String) method of PrintWriter Class in Java is used to print the specified String on the stream and then break the line. This String is taken as a parameter. Syntax: public void println(String string) Parameters: This method accepts a mandatory parameter string which is the String to be printed in the Stream. Return Value: This method d
2 min read
PrintWriter println(Object) method in Java with Examples
The println(Object) method of PrintWriter Class in Java is used to print the specified Object on the stream and then break the line. This Object is taken as a parameter. Syntax: public void println(Object object) Parameters: This method accepts a mandatory parameter object which is the Object to be printed in the Stream. Return Value: This method d
2 min read
PrintWriter println() method in Java with Examples
The println() method of PrintWriter Class in Java is used to break the line in the stream. This method do not accepts any parameter or return any value. Syntax: public void println() Parameters: This method do not accepts any parameter. Return : This method do not returns any value. Below methods illustrates the working of println() method: Program
2 min read
PrintWriter println(char[]) method in Java with Examples
The println(char[]) method of PrintWriter Class in Java is used to print the specified character array on the stream and then break the line. This character array is taken as a parameter. Syntax: public void println(char[] charArray) Parameters: This method accepts a mandatory parameter charArray which is the character array to be printed in the St
2 min read
PrintStream println(float) method in Java with Examples
The println(float) method of PrintStream Class in Java is used to print the specified float value on the stream and then break the line. This float value is taken as a parameter. Syntax: public void println(float floatValue) Parameters: This method accepts a mandatory parameter floatValue which is the float value to be written on the stream. Return
2 min read
PrintStream println(long) method in Java with Examples
The println(long) method of PrintStream Class in Java is used to print the specified long value on the stream and then break the line. This long value is taken as a parameter. Syntax: public void println(long longValue) Parameters: This method accepts a mandatory parameter longValue which is the long value to be written on the stream. Return Value:
2 min read
Article Tags :
Practice Tags :