Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

diff between System.out and System.err

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the actual difference bwtween system.out.println and system.err.println()
 
Henry Wong
author
Marshal
Pie
Posts: 21378
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by saikrishna cinux:
what is the actual difference bwtween system.out.println and system.err.println()


The first writes to "standard output". The second writes to "standard error". These two streams are setup by the operating system when the Java program is executed... And can be redirected with the uses of "|", ">", etc. on the command line.

If the Java program is just ran without any redirection (and the java program don't redirect them), there should be no difference.

Henry
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is neither system.out.println(), nor system.err.println().

Extending Henrys answer,


is guaranteed to print "Hello World", but

might print "llo He World" or "HeWorld llo" too.

[ May 13, 2006: Message edited by: Stefan Wagner ]
[ May 13, 2006: Message edited by: Stefan Wagner ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henery/Stefan,

Could you please eloborate some more about the System.err and System.out .....


It's really sound very much interesting for me.



 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't much to elaborate on that.

If you got an errormessage you want to put out to console, write it to System.err, and normal output to System.out, so the user can distinguish them.

Assume a program which searches for email-Adresses and a second one, which writes an email to an adress.


You redirect the output to another program as input:

The second program would try to write a mail to "Unknown shortcut: Virginie".
But the user can use redirection:

The message "Unknown shortcut: Virginie" would go to the logfile, and only the valid output would be used as input for the second program.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
There is neither system.out.println(), nor system.err.println().

Extending Henrys answer,


is guaranteed to print "Hello World", but

might print "llo He World" or "HeWorld llo" too.

[ May 13, 2006: Message edited by: Stefan Wagner ]

[ May 13, 2006: Message edited by: Stefan Wagner ]


No boss i nerver see such thing happen in my system.
it is displaying correctly
the order is correct




cinux
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankur Sharma:
Henery/Stefan,

Could you please eloborate some more about the System.err and System.out .....


It's really sound very much interesting for me.






Hai

Its very simple,

Every operating system opens file handles for system input , output and error. the programming languages links your application to that handles.

System.out. --------> output handle ( mostly conslole ).
System.err --------> linked to both err file or some times to console

ofcourse you can change the file handle to which these are pointing.....

read some operation stuff( preferrebly unix)..

All the best....
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[cinux]: No boss i nerver see such thing happen in my system.
it is displaying correctly
the order is correct


The fact that it displays correctly on your system is no guarantee for other systems. On many systems, standard output and standard error will use different buffers, and these buffers may be flushed at different times. On my own system just now I got:

HeWorld
llo
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally STDERR is not buffered, STDOUT is.

The last thing STDERR writes do is internally call flush().

If you intermingle the two, e.g.:



it is possible (likely) to see error messages before you see what actually caused the error.

Guy
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Srinivas Kalvala:




System.out. --------> output handle ( mostly conslole ).
System.err --------> linked to both err file or some times to console



you r saying tht it saves in the error file
where can i get tht file in my system???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic