• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

System.out v/s System.err

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following program :


public class kaushal
{

public static void main(String[] args)
{

System.out.println("xyz");
System.err.println("xyz2");
System.err.println("xyz2");
System.err.println("xyz3");
System.out.println("xyz");
System.out.println("xyz");
System.err.println("xyz3");
}
}

As shown I have used err and out class and when I run this program in NetBeans a strange behaviour is seen :

The output is :

xyz
xyz2
xyz2
xyz3
xyz3
xyz
xyz

Sometimes it would come in proper sequence sometimes in odd.

Whereas if I use the console version no matter how many times I try the output is in proper sequence.

Can someone please explain this odd behaviour in NetBeans.

I am using Netbeans 6.7.1.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i ran it in Eclipse, here's the output:

xyz2
xyz2
xyz3
xyz3
xyz
xyz
xyz

hmm...
 
author & internet detective
Posts: 41407
854
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kaushal,
Welcome to JavaRanch!

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
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow..., that's why you are a 'Sheriff'. could you please explain or give a link on the concept of a 'stream'. BTW what IDE do you use Jeanne?
 
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

venu chakravorty wrote: . . . BTW what IDE do you use Jeanne?

If you are a real beginner, you shouldn't ask that sort of question. You should avoid IDEs until you are more experienced.

If you are more experienced, you should try different IDEs and see which you are happiest with. Some people like one, some people are happier with another.
 
Venu Chakravorty
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, i'll keep that in mind Campbell.
 
Kaushal Wadhwani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks sheriff.....i got dat....

can you please tell me how you get to know about this...i tried at many places but couldnt get a satisfactory answer regarding this...

wherever i read, this thing about streams was not written..i tried java.sun.com and other places like these
 
Venu Chakravorty
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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, so why do we see different outputs?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jeanne Boyarsky
author & internet detective
Posts: 41407
854
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kaushal Wadhwani wrote:wherever i read, this thing about streams was not written..i tried java.sun.com and other places like these


You saw it written here . Seriously though - if you want something more authoritative, try the System JavaDoc. It shows that both System.out and System.err are of the PrintStream class.

venu chakravorty wrote:BTW what IDE do you use Jeanne?


For Java - Eclipse. But that doesn't mean anything. I also code in vi when shell scripting because it's what's available.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Ta
Murali
 
Kaushal Wadhwani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic