System.out.println() in Java. How does it work?

System.out.println(exp); is used to display messages to the command window. If we go further to the functioning of System.out.println() statement, we will find that:

1. System is a class built into the core Java language and it is defined within the java.lang package.

2. out is a public static member of the System class, of type PrintStream. Thus, the expression System.out refers to an object of type PrintStream.

3. The (overloaded) println method of the PrintStream class accepts an expression as an argument and displays it in String form to the standard output window (i.e., the command-line window from which the program was invoked). There are multiple println overloaded methods with different arguments. Every println makes a call to print method and adds a newline. Internally, print calls write() and write() takes care of displaying data to the standard output window.

We therefore don't need to ever instantiate a System object to print messages to the screen; we simply call the println method on the System class's public static PrintStream member, out.

Now, you might be thinking that can we create an object of PrintStream and call println function with that object to print to the standard output (usually the console)? The answer is NO. When you want to print to the standard output, then you will use System.out. That's the only way. Instantiating a PrintStream will allow you to write to a File or OutputStream you specify, but don't have anything to do with the console.

However, you can pass System.out to PrintStream and then invoke println on PrintStream object to print to the standard output. Following is a small example:

import java.io.*;
public class SystemOutPrintlnDemo
{
  public static void main(String[] args) 
  {
    //creating PrintStream object
    PrintStream ps = new PrintStream(System.out);
    ps.println("Hello World!");
    ps.print("Hello World Again!");
    //Flushes the stream
    ps.flush();
  }
}
 
OUTPUT
======
D:\JavaPrograms>javac SystemOutPrintlnDemo.java
D:\JavaPrograms>java SystemOutPrintlnDemo
Hello World!
Hello World Again!

Hope you have enjoyed reading about the working of System.out.println. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!




Get Free Tutorials by Email

About the Author

is the main author for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.

Today's Tech News

New seat alert app for pregnant commutersPosted on Sunday January 22, 2017

Developers launch a new app to help pregnant women get a seat on public transport while commuting.

How we built India's biggest robot companyPosted on Sunday January 22, 2017

GreyOrange is India's biggest robotics company, making machines which support the country's booming online retail industry.

Nintendo signals end for Wii UPosted on Friday January 20, 2017

Nintendo signals the "end of life" for its Wii U games console, which was first released in 2012.

Courtesy BBC News