Make Selenium Easy

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.

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:-

println()
println(boolean)
println(char)
println(char[])
println(double)
println(float)
println(int)
println(long)
println(java.lang.Object)
println(java.lang.String)

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.

Output:-

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

#ThanksForReading

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.