Java Interview Question – Explain System.out.println(arg)

Printing information while executing program is important. Like any other programming language, Java also provides a way to print desired thing on standard output.

System.out.println(arguments);” is a java statement to print passed argument on standard output stream which is by default console. We can also print in to a file which we will see later in this post. First we will see why do we write above statement and understand each word of it.

First of all above statement is using chaining concept. Instead of creating reference chaining mechanism is used.

// Using chaining
// Without chaining
PrintStream ps= System.out;
*Replace 'ps' with 'System.out' for chaining.

Above both statements does the same thing.

Now let’s understand each term of above statement:-

  1. “System”: System is a final class in Java. System class provides facilities for standard input, standard output, and error output streams i.e. reading from standard input (e.g. Keyboard) and printing to standard output( e.g. Console, File) .
  2. out“: It starts with small “o”. “out” is a final static variable of type class “PrintStream” . It is a standard output stream. This stream is already open and ready to accept output data. Please note that a static member of class can be called using class name.
  3. println()“: This method is in PrintStream class. “println()” is overloaded method to support all types of arguments. Below are the overloaded methods:-


It will print whatever type of argument you pass. Like println(“Java”) will print “Java”.

It is also important to learn about its working mechanism. PrintStream class has overloaded “print()” methods same as “println()”. Every println() method calls “print()” method internally followed by newline() method. “newline()” method terminates the current line of printing. It means next print statement will print in next line.

Source code of println(String):-

Source code of print(String):-

This also answers difference between “print()” and “println()” methods.

Changing standard output to a file:-

System class provides a method to reassign the standard output stream.

package TheoryPrograms;


public class ChangingStandardOutputToFile {
	public static void main(String[] args) throws FileNotFoundException {
		System.out.println("This will be printed to console.");
		// Reassigning standard output to file. File will be created automatically
		// if not available.
		System.setOut(new PrintStream(new File(System.getProperty("user.dir")+"/log.txt")));
		System.out.println("This will be printed to file.");



Hope, I am able to explain in details. If you have any doubt, concerns or feedback, do let me know.


Leave a Reply

Your email address will not be published. Required fields are marked *