System.out.println

29/04/2012

It is love at first type. I fell in deep love with it from the first moment I used it. Didn’t you? How many times have we used it till now? It is one of the most number of times compiled statement in the history of java. We fondly call it SOP. If you want to dive directly to topic, jump to the first heading.

I first learnt DBase III+ and the statement to print is ? "Hello World". Then I went on to to study umpteen number of languages courtesy a poorly planned curriculum. Instead, we should have spent more time on flip-flops.

At the end of this article I have summarized Pascal, C, C++, Lisp, Prolog, Cobol, Basic, Fortran – I studied all these in a 3 year post graduate program and more than that. As I told, I am still not sure of the difference it made. Learning multiple languages is good, but my humble opinion is, do it after spending more time on fundamentals like data structures, algorithms, discrete maths, computer architecture, etc. Okay, where did I left? Lets continue with our System.out.println for now.

What is System.out.println

System.out.println prints the argument passed, into the System.out which is generally stdout.

  • System – is a final class and cannot be instantiated. Therefore all its memebers (fields and methods) will be static and we understand that it is an utility class. As per javadoc, “…Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array…”
  • out – is a static member field of System class and is of type PrintStream. Its access specifiers are public final. This gets instantiated during startup and gets mapped with standard output console of the host. This stream is open by itself immediately after its instantiation and ready to accept data. When running a program from windows command line, it is the standard console.
  • println – println prints the argument passed to the standard console and a newline. There are multiple println methods with different arguments (overloading). Every println makes a call to print method and adds a newline. print calls write() and the story goes on like that.

Change out of System.out.println

‘out’ object can be customized. out gets initialized by java runtime environment at startup and it can be changed by developer during execution. Instead of standard output, in default cases when you run a program through command line, the output is printing in the same command window. We can change that behavior using setOut method as below. In the following example, I have redirected the output to a text file in the same directory.

public class ChangeOut {	public static void main(String args[]) {		try {			System.setOut(new PrintStream(new FileOutputStream("log.txt")));			System.out.println("Now the output is redirected!");		} catch(Exception e) {}	}}

System.out.println vs loggers like Log4j

Log4J has mulitple levels for logging. If we are writing a real short program, just for experimental/learning purposes SOPs are fine. When we are developing a production quality software project, we should be aware that a professional logger should be used and SOPs should be avoided. Why?

  • Flexibility: a professional logger like log4j provides different levels for logging. We can seggregate the log message accordingly. For example, X messages should be printed only on PRODUCTION, Y messages should be printed on ERROR, etc.
  • Configurability: in just one parameter change we can switch off all the logging statements.
  • Maintainability: imagine if we have hundreds of SOPs littered all through the application, it would be difficult to maintain the program over a period.
  • Granularity: In an application, every single class can have a different logger and controlled accordingly.
  • Utility: Option for redirecting the message is limited in System.out, but in case of a logger you have appenders which provides numerous options. We can even create a custom ouput option and redirect it to that.

Having said all the above, we still use System.out.println for logging and debugging. Which should be strictly avoided. This is driven by (bad)habit. I want to share how a habit became a convention. In this case its a good habit. Its using ‘i’, ‘j’ as index in for-loop. In FORTRAN language, we need not declare integer variables. Variable names that start with i, j, k, l, m and n are integer variables. So, FORTRAN developers named for-loop index with i,j,k and that habit carried on to other languages. Just a habit and became a good convention!

System.out.println and Performance

There is a general notion that SOPs are bad in performance. When we analyze deeply, the sequence of calls are like println -> print -> write() + newLine(). This sequence flow is an implementation of Sun/Oracle JDK. Both write() and newLine() contains a synchronized block. Synchronization has a little overhead, but more than that the cost of adding characters to the buffer and printing is high.

When we run a performance analysis, run multiple number of SOP and record the time, the execution duration increases proportionally. Performance degrades when we print more that 50 characters and print more than 50,000 lines.

It all depends on the scenario we use it. Whatever may be the case, do not use System.out.println for logging to stdout.

Static Import to Shorten SOP

Someone complained that System.out.println is a loooong statement to print something. static import may shorten it a bit but it is not recommended, because it results in poor readability. I am just using this situation to explain static import and avoid using it in the below scenario.

import static java.lang.System.out;public class ShortSOP {public static void main(String[] args) {out.println("Hello, world");}}

In Eclipse you have programmed shortcuts like ctrl + spac to help you out.

System.err and System.in

As a related section, I wish to discuss about ‘err’ and ‘in’. ‘in’ is associated with InputStream. Opposite to ‘out’, ‘in’ is used to get input from standard console generally keyboard.
‘err’ is associated with PrintStream and prints the argument to the standard error output stream. When you use eclipse kind of IDE you can see the differene in ouput between ‘out’ and ‘err’.

public class InOutErr {public static void main(String args[]) {try {BufferedReader reader = new BufferedReader(System.in);String filename = reader.readLine();  InputStream input = new FileInputStream(filename);  System.out.println("File opened...");} catch (IOException e){  System.err.println("Where is that file?");}}}

Print in other languages

DBASE III+
? "Hello World"

C

 #include <stdio.h> #include <stdlib.h> int main(void) {  printf("Hello, world");  return EXIT_SUCCESS; }

CPP

 #include <iostream> int main() { 	std::cout << "Hello, World." << std::endl; }

BASIC

10 PRINT "HELLO WORLD"

FORTRAN

 PROGRAM HELLOWORLD 10 FORMAT (1X,11HHELLO WORLD) WRITE(6,10) END

COBOL

 IDENTIFICATION DIVISION. PROGRAM-ID. Hello. ENVIRONMENT DIVISION. DATA DIVISION. PROCEDURE DIVISION.	Display 'Hello, World'.	STOP RUN.

LISP

	(DEFUN HELLO-WORLD ()	(PRINT (LIST 'HELLO 'WORLD)))

PROLOG

go :-	writeln('Hello World').

Yes, I sourced all this programs from Internet 🙂 How come you expect me to remember all these! System.out.println(“Bye”);

Source : http://javapapers.com/core-java/system-out-println/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s