13

I used to write & develop codes with system.out.println(). it usually helps me track values and where the problem is coming. And after developed application, I don't remove the system.out.println() because it might helpful after user found any issue it will come back to us, which makes it so easy to track where went to wrong. But one of my superior suggested to remove system.out.println() from codes because its effect the code efficient level. Is this correct?

From my point of view, System.out.print() hardly take bytes on memory, so is it true that developer shouldn't use much system.out.println ??

Thanks in advance.

Rudy
  • 7,008
  • 12
  • 50
  • 85
Will Mcavoy
  • 485
  • 5
  • 24
  • 1
    See http://stackoverflow.com/questions/8601831/do-not-use-system-out-println-in-server-side-code – devnull May 28 '13 at 05:55
  • you should use logger rather than syso lines – Hussain Akhtar Wahid 'Ghouri' May 28 '13 at 06:00
  • 1
    also note that when you are using a logging framework like apache commons, you should check whether the log level is enabled before trying to log concatenated strings. Calling Logger.debug("x = " + 5) if the log level is only INFO would cause a string concatenation (which is heavy) while the message you tried to log does not really get logged. –  May 28 '13 at 06:04

9 Answers9

21

System.out implementation contains a synchronized block on the output stream.

From PrintStream.java :

      /**
        * Prints a String and then terminate the line.  This method behaves as
        * though it invokes <code>{@link #print(String)}</code> and then
        * <code>{@link #println()}</code>.
        *
        * @param x  The <code>String</code> to be printed.
        */

       public void println(String x) {
           synchronized (this) {
               print(x);
               newLine();
           }
      }

Putting System.out.println a lot will make the entire project to run almost single-threaded, because all thread will waiting for synchronization lock, and make your application begin to crawl.

This means your superior is right.

Alternatively, Use logging framework like log4j instead. You can still configure your log4j to still output to System.out.println by just minor changes in the appender configuration.

Rudy
  • 7,008
  • 12
  • 50
  • 85
  • Could you please give a reference of the synchronized block of System.out implementation? Thanks! – Mingyu May 28 '13 at 06:33
3

You can use logger to solve the problem.

Specify the level of logging while developing your code, and you can set level of logging in production.

List of levels:

DEBUG Level

INFO Level

WARN Level

ERROR Level

FATAL Level

Log4j is common one to use, see here:

http://logging.apache.org/log4j/2.x/

Regarding the cons of using println:

The primary reason against is that println slows down the speed of your program.

Mingyu
  • 31,751
  • 14
  • 55
  • 60
  • Thanks. Yes I know but for this I have to open a log file and see. for example while debugging the values system.out.print() just print the values. so its more convenient than log files.. what is the harm if I use more s.o.p? thats my primary objective? – Will Mcavoy May 28 '13 at 06:01
  • println slows down the speed of your program in production – Mingyu May 28 '13 at 06:08
3

use of logging frameworks such as SLF4J is highly recommended. That way by configuration, you can execute these logging statements based on the environement.

eg - DEBUG level on Developer -- WARN on Production, so only critical iformation is logged in production.

Sanath
  • 4,774
  • 10
  • 51
  • 81
2

It is true that outputting to the console, especially excessive output, will reduce the application's performance. It's also better programming style not to burden the user with information that only you can use. Instead, you should use Java's in-built mechanisms for handling unexpected events, e.g. exceptions and the assert mechanism. You can also choose to have a 'verbose' option as a command-line argument.

zetches
  • 148
  • 8
2

System.out.println can cause serious performance issues if used extensively in a multi-threaded environment (even webapplications). Every println call acquires a lock so that message displayed on the console is not mixed up with other messages.

Also in most cases the output of the console is redirected to a file. So it incurs a lot of synchronous IO operations that can lead to performance degradation.

gkamal
  • 20,777
  • 4
  • 60
  • 57
2

Yes it is true that calls to System.out.println() must be minimized. This is because printing to the console is an IO operation and IO take a relatively long time to complete. This is why as much as possible, we use a StringBuilder to concatenate the stuff we want to print in memory. After pooling everything we want to print in memory, this is the time in which you would want to call System.out.println().

Calling System.out.println() has no effect on memory.

1

It is good to maintain logging files rather than system.out.println.If you use this only devolopers can see on the console.You can not see more messages with this.

But you can not use it for future use.If you use logging you can save the messages into database or files ....etc

PSR
  • 39,804
  • 41
  • 111
  • 151
1

I believe that you are speaking about Java in this context and going to illustrate another method against system.out.println().

System.out.print(); // Prints a string of characters only
System.out.println(); // Prints a string of character followed by a line break and that line break is the one which is taking up tons of space if I ain't wrong.

Hope this helps.

1

putting System.out.println its very worst practice. try to implement logging concept. Use log4j OR log4j2 for mantaining the log of your system. for tracing or debugging the code.

KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52