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
System.out.println("123");
// Without chaining
PrintStream ps= System.out;
ps.println("123");
*Replace 'ps' with 'System.out' for chaining.
Above both statements does the same thing.
Now let’s understand each term of above statement:-
- “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) .
- “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.
- “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)