System.out and System.err are streams. They have the option of writing immediately or buffering and writing later. And even if they write immediately, you have a threading problem of how long it takes to be "immediate."
As a result, it's like having two people writing to the same place. You are guaranteed that each of the writers will have its output in the correct order, but not that they will between them.
venu chakravorty wrote:ok, 2 streams are opened and they try to write simultaneously... fine, but whatever happens should be same weather the program is run under an IDE or the terminal
Incorrect, applications are not guaranteed to behave the same under all environments. The JVM must implement the Java Specifications for the run-time environment, and the contracts created by the Java API, but nothing more, and those specs leave a lot out - for example they don't guarantee much of of anything about order of operations between threads, except where synchronized barriers are concerned.
An IDE may be using a different JVM than your terminal window, and if using the same one it could be using different run-time parameters. So there should be no expectation that the IDE behaves just like the terminal window.
, so why do we see different outputs?
Because you are running the application in 2 different and unrelated environments.
author & internet detective
System.out messages can be written to a file where as System.err could not.
If you run the program like java kaushal ->kaushal .txt, you could see the System.out messages in kaushal .txt and System.err messages in the console.
Joined: Aug 22, 2009
I went on to the System JavaDoc link given by you Sheriff but what I was looking for was an official explanation about the behaviour of the classes regarding buffering and threading.
I just mean to ask, is this an assumption based on the output that we observe or is this a concrete explanation regarding System.out and System.err classes.