Tutorial :Should I use System.out.println() or something else?


I've been casually programming in Java for a while, but I still have a few burning questions on the fundamentals. I've heard that I should use System.out.println() to display data from some people, and others have given me different ideas (like PrintStream or something else). What's the best way to print to console in Java?


System.out is a PrintStream.

If your main goal is to interact with a console, look at java.io.Console. If your main goal is to have some kind of logging, use a logging framework like the Java Logging API or log4j.


If you are just starting and wanting to print some strings to the console then System.out.println() will be fine. Get your fundamentals down and then you can dive into the finer points of Java I/O.


System.out is good for printing to the console. However, I found that making the effort to use java.util.logging instead was a better choice for me. (Or some logging system.)

It depends on what information you're attempting to display. If it's for console user-interaction, System.out is perfect. If it's to display debug-ish information, logging is probably the way to go as it lets you have far more control over things should you desire it.

(After the initial set-up, the logging utilities are almost as transparent as using System.out.)


Kind of depends on what you are doing. Almost always I would suggest using the Java logging API (java.util.logging). System.out.println is more of a "debug tool" but even then you have much more control over the output using the JDK's logging API...


Use log4j and direct output to stdout. Its then easy to switch to logfiles as required.


Using System.out directly is the simplest and usually most efficient way. If yo have a lot to be printed you might investigate using BufferedOutputStream.

There are some efficiencies to be gained by creating the Strings to be printed more efficiently. If there is a lot of concatenation being done, look into StringBuilder.


yu can set

System.setOut( new PrintStream( "all-my-sysouts-go-in-this-file.txt" );

and then do sysoyts...

yu can track them using baretail...

hope this helps..



I will suggest to use Java Logging APIs


Yes, the simplest and efficient way to print to the console is usually System.out.print() and System.out.println() but if you have a large amount of output to send, System.out.println() may be inefficient, because it is line-buffered and does a lot work related to Unicode handling. Because of its small buffer size, System.out.println() is not well suited to handle many repetitive outputs in a batch mode. Each line is flushed right away. In that case, you may want to use a BufferedOutputStream or a BufferedWriter.

It is important to note that System.out does not always go to the application console or terminal. System.out points to the application standard output channel; in C/C++ it is called stdout. That may go to the console, but it may also go anywhere that the user decides to send it. For example a file then everything printed to System.out will end up in the file my-app-output.txt.

So, if you want to send regular output, use System.out. If you want to send an error message or user notification, with a great chance of going to the console, use System.err.print() and System.err.println().

There is no portable way to get a channel to the 'real' console in versions of Java before 1.6. In 1.6, there is an object class specifically to represent the console: java.io.Console. A reference to this unique object can be obtained from the System class using the static method console().

But If your requirement is to debug the whole application then as everyone said using loggers is the best approach.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »