difference between System.out.println() and System.err.println()
Asked Answered
P

7

67

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

Priscian answered 2/7, 2010 at 5:23 Comment(0)
C
86

In Java System.out.println() will print to the standard out of the system you are using. On the other hand, System.err.println() will print to the standard error.

If you are using a simple Java console application, both outputs will be the same (the command line or console) but you can reconfigure the streams so that for example, System.out still prints to the console but System.err writes to a file.

Also, IDEs like Eclipse show System.err in red text and System.out in black text by default.

Castile answered 2/7, 2010 at 5:26 Comment(3)
Also, System.out is buffered, and System.err is notFibrinolysis
Not dissing your answer but it's normally referred to as standard out and standard error (stdout/stderr) rather than standard out and error out. +1 by way of apology in advance since, other than that nitpick, you're correct.Caseinogen
You are right, but I couldn't by god remember the right names... Using C# has clearly been bad for my low-level programming skillsCastile
N
40

System.out is "standard output" (stdout) and System.err is "error output" (stderr). Along with System.in (stdin), these are the three standard I/O streams in the Unix model. Most modern programming environments (C, Perl, etc.) support this model.

The standard output stream is used to print output from "normal operations" of the program, while the error stream is for "error messages". These need to be separate -- though in most cases they appear on the same console.

Suppose you have a simple program where you enter a phone number and it prints out the person who has that number. If you enter an invalid number, the program should inform you of that error, but it shouldn't do that as the answer: If you enter "999-ABC-4567" and the program prints an error message "Not a valid number", that doesn't mean there is a person named "Not a valid number" whose number is 999-ABC-4567. So it prints out nothing to the standard output, and the message "Not a valid number" is printed to the error output.

You can set up the execution environment to distinguish between the two streams, for example, make the standard output print to the screen and error output print to a file.

Neural answered 2/7, 2010 at 5:44 Comment(1)
I think this is the best answer, as it explains the meaning of the streams.Brewton
H
4

Those commands use different output streams. By default both messages will be printed on console but it's possible for example to redirect one or both of these to a file.

java MyApp 2>errors.txt

This will redirect System.err to errors.txt file.

Haldane answered 2/7, 2010 at 5:26 Comment(1)
java MyApp 2>errors.txt what is 2 > and how it works? can you explain?Mouth
M
2

System.out's main purpose is giving standard output.

System.err's main purpose is giving standard error.


Look at these

http://www.devx.com/tips/Tip/14698

http://wiki.eclipse.org/FAQ_Where_does_System.out_and_System.err_output_go%3F

Mestee answered 2/7, 2010 at 5:28 Comment(0)
G
1

System.out.println("wassup"); refers to when you have to output a certain result pertaining to the proper input given by the user whereas System.err.println("duh, that's wrong); is a reference to show that the input provided is wrong or there is some other error.

Most of the IDEs show this in red color (System.err.print).

Greenwood answered 16/3, 2019 at 3:39 Comment(0)
T
1

this answer most probably help you it is so much easy System.err and System.out both are the same both are defined in System class as reference variable of PrintStream class as public final static PrintStream out = null; and public final static PrintStream err = null;

means both are ref. variable of PrintStream class. normally System.err is used for printing an error messages, which increase the redability for the programmer.

A minor difference comes in both when we are working with Redirection operator.

Trackandfield answered 16/3, 2019 at 8:9 Comment(0)
U
-3

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");
    }
}
Unemployable answered 29/1, 2017 at 10:1 Comment(1)
"An OS has one queue for both System.err and System.out." That's a very general and not necessarily accurate statement. On what basis do you make that statement? [Citation needed] In a console application (such as cmd or terminal), both streams ("queues" as this answer calls them) appear to be merged (because they show in the same terminal emulator), but they are actually separate.See Difference between stdout, stderr and stdin for details.Overwhelm

© 2022 - 2024 — McMap. All rights reserved.