• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can anyone explain this please ??

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please take a look at following code.

class Foo {

static boolean truth() {
System.err.println("truth Called");
return true;
}

static final boolean TRUTH = truth();

Foo() {
System.out.println("Foo Const");
}
}

public class Test {
public static void main(String args[]) throws Throwable {
new Foo();
}
}


When I ran this code, it had produced following output(Which I was Expecting)

truth Called
Foo Const

When next time I ran that code it had produced following output(Which I was not expecting)

Foo Const
truth Called

Is output is buffered before it gets printed on console ???

[ June 24, 2007: Message edited by: Bharat Makwana ]
[ June 24, 2007: Message edited by: Bharat Makwana ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something to do with the different PrintStreams (System.err and System.out)...?

If this behavior is reproducible for you (it's not for me), then you should be able to determine if it's the PrintStreams by switching err and out.
[ June 25, 2007: Message edited by: marc weber ]
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you Mr.weber
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well!!
please if anyone can tell me if i am right here,in the below shown code,the class test calls the the constructor of Class Foo
so it prints Foo const

class Foo {

static boolean truth() {
System.err.println("truth Called");
return true;
}

static final boolean TRUTH = truth();

Foo() {
System.out.println("Foo Const");
}
}



public class Test {
public static void main(String args[]) throws Throwable {
new Foo();
}
}



but it prints "truth called" is it because it is in static block it is called automatically as the class is instantiated?

please let me know if i am correct or there is some other reason for it?
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not understand how will using err or out will affect output.

I think it should be,

truth called
Foo const

regardless of what I use , err or out
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dhwani: Yes, static variables are initialized when the class is loaded, which happens when the constructor is called but before the body of the constructor executes. This is why we would expect "truth Called" to be output before "Foo Const."

Bharat: I've tried this code many times on different machines (Mac and Windows), and I have not observed the odd behavior. Are you able to reproduce this? Are you able to change the behavior by switching PrintStreams?
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc for clearing my doubt,but i tried running the same program on my PC using eclipse IDE at first it printed

truth called
Foo const
but second time can say again and again as i was running or executing the program the order of truth called and Foo const is changing
somtimes it prints

truth called
Foo const

and somtimes it prints

Foo const
truth called

i am a bit confused here any time can this thing affect the output of other programs as well,if this kind of strategy is being used like somewhere i use System.err and somewhere System.out?
will the sequence of output change?

please if you could highlight on this point it will be kinda of you.
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi marc.

I have run this code many times on window.

When I use 'err' and 'out' then output is not predictable but when I use 'out' in both places output is predictable i.e
truth called
Foo Const

And with 'err' and 'out',sometimes it's

Foo Const
truth called

and sometimes
truth called
Foo Const

 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bharat Makwana:
... When I use 'err' and 'out' then output is not predictable but when I use 'out' in both places output is predictable...


It seems like there's some multithreading going on, but I just don't know. I couldn't find anything on this last night. I'll do some more looking.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect it's just the OS doing its thing. In traditional stdio implementations, stdout is line-buffered, and stderr is unbuffered, so the "dwell time" for stuff you write is going to be different -- i.e., writing newline to stderr isn't necessarily going to flush the data to the screen, but for stdout, that might very well be the case.

My prediction is that if you add

System.err.flush()

before returning from truth(), you'll see "truth called" printed first every single time.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been on at least one system where flush() didn't seem to fix this problem. You may need to write everything to the same stream. E.g. replace each System.err with System.out. Or call

System.setErr(System.getOut());

at the beginning of your program. Or use the command line to redirect error to standard out. This seems to work on both Windows and Unix-based systems (those I've tried, anyway).

java MyProgram 2>&1

Roughly, this means send file descriptor 2 (the error stream) to the same place file descriptor 1 (standard out) is pointing to.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well!!
Mr Ernest

i tried using System.err.flush();
but it is still giving the same output as explained by me earlier.
Please can you tell me any reason for not printing the output in the same order?
it will be kinda of you.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What OS and Java version are you using?

(My thanks to EFH and Jim for joining in. )
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok!!!
i am using windows XP operating system and java 1.6 version i am using....
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what EFH is saying could be the reason.

According to my knowledge....

Normally err and out point to the same output device the screen.(you can change it though)

Now the both are buffers internally(for obvious reason of abstracting the output device from user programs)....usually the buffers are dumped when they are full...OS is conservative and does not output every byte that is written to the buffer...I think this might have to do something with the problem that our friend is facing...
 
reply
    Bookmark Topic Watch Topic
  • New Topic