This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Stumbled across something strange today Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Stumbled across something strange today" Watch "Stumbled across something strange today" New topic
Author

Stumbled across something strange today

Danjel Nyberg
Greenhorn

Joined: Sep 12, 2012
Posts: 15

I was just tweaking a bit and stumbled across something strange, which i can't explain

given this simple class:


this gives different output every time i run it...

expected output is:

0
1
2
3
4
Exception in thread "main" java.lang.Exception
at SCJP.Test18.main(Test18.java:9)

but if I run it several times it could give me this output:

0
1Exception in thread "main" java.lang.Exception
at SCJP.Test18.main(Test18.java:9)

2
3
4

My only explenation to this is that the update of output to the Eclipse console is not keeping up.

Any ideas or input?


/Danjel
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

It gives me all time first output.Although my thought about your second output, i don't care, it isn't topic of research.


OCA7
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

It's because the System.out.println() invocations go to the standard output stream (System.out) and the stack trace goes to the standard error stream (System.err). Two different buffered streams, which may or may not get flushed in the same order you invoke them. A simple way to make sure the output is always ordered the way you expect, in a single-threaded application, is to assign the same output stream to both System.out and System.err, using the System.setOut() and System.setErr() methods.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
 
jQuery in Action, 2nd edition
 
subject: Stumbled across something strange today